split.pdf
(
56 KB
)
Pobierz
split
split
takes a vector or other objects and splits it into groups determined by a factor or list of
factors.
> str(split)
function
(x, f, drop =
FALSE,
...)
·
x
is a vector (or list) or data frame
·
f
is a factor (or coerced to one) or a list of factors
·
drop
indicates whether empty factors levels should be dropped
6/14
split
> x <- c(rnorm(10), runif(10), rnorm(10,
1))
> f <- gl(3,
10)
> split(x, f)
$‘1‘
[1] -0.8493038 -0.5699717 -0.8385255 -0.8842019
[5]
0.2849881 0.9383361
-1.0973089
2.6949703
[9]
1.5976789
-0.1321970
$‘2‘
[1]
0.09479023 0.79107293 0.45857419 0.74849293
[5]
0.34936491 0.35842084 0.78541705 0.57732081
[9]
0.46817559 0.53183823
$‘3‘
[1]
0.6795651 0.9293171 1.0318103 0.4717443
[5]
2.5887025 1.5975774 1.3246333 1.4372701
7/14
split
A common idiom is
split
followed by an
lapply
.
> lapply(split(x, f), mean)
$‘1‘
[1]
0.1144464
$‘2‘
[1]
0.5163468
$‘3‘
[1]
1.246368
8/14
Splitting a Data Frame
>
library(datasets)
> head(airquality)
Ozone Solar.R Wind Temp Month Day
1
41
190 7.4
67
5
1
2
36
118 8.0
72
5
2
3
12
149 12.6
74
5
3
4
18
313 11.5
62
5
4
5
NA
NA 14.3
56
5
5
6
28
NA 14.9
66
5
6
9/14
Splitting a Data Frame
> s <- split(airquality, airquality$Month)
> lapply(s,
function(x)
colMeans(x[, c("Ozone",
"Solar.R", "Wind")]))
$‘5‘
Ozone Solar.R
Wind
NA
NA 11.62258
$‘6‘
Ozone
Solar.R
Wind
NA 190.16667 10.26667
$‘7‘
Ozone
Solar.R
Wind
NA 216.483871
8.941935
10/14
Plik z chomika:
mileymandy
Inne pliki z tego folderu:
Subsetting.pdf
(100 KB)
tapply.pdf
(62 KB)
reading_data_II.pdf
(84 KB)
Scoping.pdf
(185 KB)
reading_data_I.pdf
(101 KB)
Inne foldery tego chomika:
●● BIZNES i PRACA
Absolwentka
Ameryka. Historia USA
analiza matematyczna
Auchan
Zgłoś jeśli
naruszono regulamin