要找到行意味着可以使用rowMeans函数,但是如果数据帧中缺少一些值,则可以使用na.rm = TRUE自变量,方法与计算列的均值时所使用的方法相同。例如,如果我们有一个数据帧df,其中包含两列x和y,每列都有一些缺失值,则行均值可以计算为rowMeans(df,na.rm = TRUE)。
请看以下数据帧-
set.seed(1515) x1<-sample(c(NA,1,25,31),20,replace=TRUE) x2<-sample(c(NA,5,12,27),20,replace=TRUE) x3<-sample(c(NA,15),20,replace=TRUE) x4<-sample(c(NA,15,9),20,replace=TRUE) df1<-data.frame(x1,x2,x3,x4) df1
输出结果
x1 x2 x3 x4 1 25 NA NA NA 2 25 12 15 NA 3 25 NA 15 NA 4 31 5 NA NA 5 31 27 15 15 6 NA 5 NA 9 7 25 12 15 NA 8 31 5 15 NA 9 1 5 15 15 10 1 27 NA NA 11 25 NA 15 NA 12 25 12 15 15 13 25 NA 15 9 14 31 NA 15 15 15 31 27 15 9 16 1 12 NA 15 17 1 NA NA 9 18 25 27 15 NA 19 31 5 15 9 20 NA 5 15 NA
找到df1的行均值-
rowMeans(df1,na.rm=TRUE)
输出结果
[1] 25.000000 17.333333 20.000000 18.000000 22.000000 7.000000 17.333333 [8] 17.000000 9.000000 14.000000 20.000000 16.750000 16.333333 20.333333 [15] 20.500000 9.333333 5.000000 22.333333 15.000000 10.000000
让我们看另一个例子-
y1<-sample(c(NA,rnorm(5,1,0.003)),20,replace=TRUE) y2<-sample(c(NA,rnorm(10,50,2.47)),20,replace=TRUE) y3<-sample(c(NA,runif(5,1,4)),20,replace=TRUE) y4<-sample(c(NA,runif(5,2,10)),20,replace=TRUE) y5<-sample(c(NA,rexp(5,3.5)),20,replace=TRUE) df2<-data.frame(y1,y2,y3,y4,y5) df2
输出结果
y1 y2 y3 y4 y5 1 0.9965744 48.73434 2.097240 9.657755 0.32815971 2 1.0003618 44.83392 2.877004 9.735341 0.27053003 3 0.9974534 NA 2.097240 9.657755 0.64288668 4 0.9999057 54.12249 2.097240 NA 0.06486254 5 1.0003618 54.12249 2.877004 5.945301 NA 6 0.9965744 NA NA NA 0.27053003 7 1.0003618 54.12249 NA 5.945301 0.06486254 8 1.0022832 44.83392 1.065712 5.945301 0.64288668 9 1.0003618 54.34290 NA 9.735341 0.64288668 10 1.0003618 NA 2.323069 3.774950 NA 11 0.9999057 54.12249 1.834897 3.774950 0.64288668 12 0.9999057 53.84937 1.834897 NA 0.44797666 13 0.9974534 47.75855 1.065712 9.735341 0.44797666 14 1.0022832 NA 1.065712 3.774950 0.32815971 15 1.0003618 54.12249 2.877004 5.945301 0.27053003 16 0.9974534 54.34290 2.323069 9.657755 0.64288668 17 NA 44.83392 1.065712 3.774950 0.32815971 18 0.9965744 54.34290 NA NA 0.06486254 19 1.0022832 49.89409 2.323069 3.774950 0.06486254 20 1.0003618 49.89409 1.065712 4.078849 0.32815971
找到df2的行均值-
rowMeans(df2,na.rm=TRUE)
输出结果
[1] 12.3628143 11.7434319 3.3488338 14.3211253 15.9862898 0.6335522 [7] 15.2832544 10.6980210 16.4303723 2.3661269 12.2750266 14.2830369 [13] 12.0010071 1.5427764 12.8431379 13.5928126 12.5006862 18.4681122 [19] 11.4118515 11.2734351