data.table
, plyr
및 기본 기능이 여기에 있습니다. 다른 많은 방법이 있습니다.
,363,210
첫째, 일부 예시적인 데이터 ...
dput(examp)
structure(list(P = structure(c(1L, 1L, 1L, 2L), .Label = c("P1",
"P2"), class = "factor"), M = structure(c(1L, 1L, 2L, 2L), .Label = c("M1",
"M2"), class = "factor"), R = structure(c(1L, 2L, 1L, 1L), .Label = c("R1",
"R2"), class = "factor"), V = c(23, 49, 24, 29)), .Names = c("P",
"M", "R", "V"), row.names = c(NA, -4L), class = "data.frame")
#
# to give something like what you have...
#
examp
P M R V
1 P1 M1 R1 23
2 P1 M1 R2 49
3 P1 M2 R1 24
4 P2 M2 R1 29
여기 data.table
를 사용하여 하나의 방법이다. http://datatable.r-forge.r-project.org/datatable-intro.pdf
# What is the average of each element of P?
library(data.table)
examp.dt <- data.table(examp)
setkey(examp.dt,P)
examp.dt[,mean(V),by=P]
P V1
[1,] P1 32
[2,] P2 29
#
그리고 다른 plyr
# What is the average of each element of P?
library(plyr)
ddply(examp, "P", function(df)mean(df$V))
P V1
1 P1 32
2 P2 29
를 사용하여 다른 사용하여 기본 R
: 데이터 오브젝트가 매우 큰 경우, 당신은 매우 빨리하기 위해
data.table
패키지를 찾을 수 있습니다, 문서도 우수
# What is the average of each element of P?
# for example using the by() function, tapply() would be similar
with(examp, by(examp, P, mean))
P: P1
P M R V
NA NA NA 32
-------------------------------------------------
P: P2
P M R V
NA NA NA 29
#
# What is the average of each element of R?
with(examp, by(examp, R, mean))
R: R1
P M R V
NA NA NA 25.33333
----------------------------------------------
R: R2
P M R V
NA NA NA 49
#
# the same, using tapply
with(examp, tapply(V, R, mean)
R1 R2
25.33333 49.00000
마지막 질문에 P1의 몇 요소가 지정된 값보다 몇 배입니까?를 사용할 수 있습니다. 6,001,823,210과 같이 : 심지어
# how many elements of P1 are greater than 20?
nrow(subset(examp, examp$P=="P1" & examp$V>20))
[1] 3
또는 적은 입력과 같은 결과 단지 [
을 :
nrow(examp[examp$P=="P1" & examp$V>20,])
[1] 3
기본 기능을 사용해보십시오. 그것은 당신에게 정말 기본적인 정보를 제공합니다. 그 중에서도 ... – Oz123
R 튜토리얼을 찾으십시오. 왜냐하면이 튜토리얼은 정말 기본적인 것들이기 때문에 여기서는 큰 답이 아닙니다. – Oz123
'tapply'와'table'을보세요. –