如果某些列在 R 数据框中是分类的,如何找到每个数字列的最小值的位置?
如果某些列在R数据框中是分类列,要找到每个数字列的最小值的位置,我们可以按照以下步骤操作-
首先,创建一个数据框。
然后,如果某些列是分类列,则使用plyr包中的numcolwise函数查找每个数字列的最小值。
示例1
创建数据框
让我们创建一个数据框,如下所示-
factor1<-sample(c("Super","Lower","Medium"),25,replace=TRUE) factor2<-sample(c("I","II","III"),25,replace=TRUE) v1<-rnorm(25) v2<-rnorm(25) df1<-data.frame(factor1,factor2,v1,v2) df1输出结果
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
factor1 factor2 v1 v2 1 Medium III -2.14379228 -0.75943453 2 Super I -1.47939801 1.59666611 3 Lower III 1.23368296 -1.38936623 4 Lower III 0.20952920 -0.04218399 5 Medium III 0.05884382 -0.84164117 6 Super II 1.96347308 0.88664585 7 Lower II 0.31493547 -1.27612944 8 Super III -1.51226987 1.29224755 9 Medium III 1.52328734 1.32171715 10 Medium I -0.16912317 0.46726813 11 Medium I 0.24834413 -0.70308350 12 Super I -1.16519280 -0.04263700 13 Medium III 0.17775948 -1.24434106 14 Super II 0.60504839 -0.50105846 15 Medium II -1.65936651 -1.57653244 16 Medium III 0.96991787 -0.62284764 17 Medium I 0.67194953 0.72451047 18 Lower III 0.73096741 -0.93229961 19 Super III -0.75207261 -1.34829579 20 Lower I -0.19563701 0.06724962 21 Lower I 0.15769044 1.08397200 22 Lower III -1.01078566 0.34309269 23 Medium I -0.16170496 0.29763892 24 Lower III 2.54521045 0.14029670 25 Lower I 0.96949903 -0.43465951
如果某些列是分类列,则查找每列的最小值
如果某些列在数据框df1中是分类列,则使用plyr包中的numcolwise函数查找每个数值列的最小值-
factor1<-sample(c("Super","Lower","Medium"),25,replace=TRUE) factor2<-sample(c("I","II","III"),25,replace=TRUE) v1<-rnorm(25) v2<-rnorm(25) df1<-data.frame(factor1,factor2,v1,v2) library(plyr) numcolwise(which.min)(df1)输出结果
v1 v2 1 1 15
示例2
创建数据框
让我们创建一个数据框,如下所示-
Level<-sample(c("low","medium","high"),25,replace=TRUE) Group<-sample(c("first","second"),25,replace=TRUE) DV1<-rpois(25,5) DV2<-rpois(25,10) df2<-data.frame(Level,Group,DV1,DV2) df2输出结果
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
Level Group DV1 DV2 1 medium second 6 12 2 medium second 5 8 3 high first 8 7 4 high second 4 17 5 medium second 4 7 6 high second 7 11 7 high second 6 12 8 medium second 8 6 9 high first 5 10 10 medium first 3 6 11 low first 5 12 12 medium second 9 15 13 low second 7 14 14 low first 5 11 15 low first 7 7 16 high second 5 11 17 high second 10 13 18 high first 7 10 19 high first 2 11 20 low second 8 9 21 medium first 4 5 22 medium first 11 13 23 low second 2 12 24 medium first 7 10 25 high first 5 10
如果某些列是分类列,则查找每列的最小值
如果某些列在数据框df2中是分类列,则使用plyr包中的numcolwise函数查找每个数值列的最小值-
Level<-sample(c("low","medium","high"),25,replace=TRUE) Group<-sample(c("first","second"),25,replace=TRUE) DV1<-rpois(25,5) DV2<-rpois(25,10) df2<-data.frame(Level,Group,DV1,DV2) library(plyr) numcolwise(which.min)(df2)输出结果
DV1 DV2 1 19 21