如何在 R 数据框中查找字符串的行模式?
要查找 R 数据框中字符串的行模式,我们可以使用 apply 函数以及用于模式的自定义函数,如果存在平局,则将根据字母顺序选择第一个值。
例如,如果我们有一个名为 df 的包含字符串值的数据框,那么我们可以使用下面给出的命令找到字符串的行模式 -
df$RowM<-apply(df[], 1, function(x) {names(which.max(table(factor(x,unique(x)))))})示例 1
以下代码片段创建了一个示例数据框 -
x1<-sample(letters[1:5],20,replace=TRUE) x2<-sample(letters[1:5],20,replace=TRUE) x3<-sample(letters[1:5],20,replace=TRUE) x4<-sample(letters[1:5],20,replace=TRUE) df1<-data.frame(x1,x2,x3,x4) df1
创建了以下数据框
x1 x2 x3 x4 1 b a c e 2 c b c a 3 b d d d 4 d a a e 5 a b c c 6 a b e e 7 d d e a 8 c b d a 9 c a c d 10 c e e a 11 d a a c 12 a b c a 13 a d d c 14 e a e b 15 d b a b 16 d a a d 17 d a a d 18 c d b c 19 c e c b 20 c e c e
要在上面创建的数据框上为 df1 中的每一行查找行模式,请将以下代码添加到上述代码片段中 -
x1<-sample(letters[1:5],20,replace=TRUE)
x2<-sample(letters[1:5],20,replace=TRUE)
x3<-sample(letters[1:5],20,replace=TRUE)
x4<-sample(letters[1:5],20,replace=TRUE)
df1<-data.frame(x1,x2,x3,x4)
df1$Row_Mode<-apply(df1[], 1, function(x) {names(which.max(table(factor(x,unique(x)))))})
df1输出
如果您将上面给出的所有代码片段作为单个程序执行,它将生成以下输出 -
x1 x2 x3 x4 Row_Mode 1 b a c e b 2 c b c a c 3 b d d d d 4 d a a e a 5 a b c c c 6 a b e e e 7 d d e a d 8 c b d a c 9 c a c d c 10 c e e a e 11 d a a c a 12 a b c a a 13 a d d c d 14 e a e b e 15 d b a b b 16 d a a d d 17 d a a d d 18 c d b c c 19 c e c b c 20 c e c e c
示例 2
以下代码片段创建了一个示例数据框 -
y1<-sample(c("Low","Medium","High"),20,replace=TRUE)
y2<-sample(c("Low","Medium","High"),20,replace=TRUE)
y3<-sample(c("Low","Medium","High"),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2创建了以下数据框
y1 y2 y3 1 High Low Low 2 Low High High 3 Low Low High 4 High Medium Low 5 High Medium Low 6 Medium Low Medium 7 Low High Low 8 Low High Low 9 Medium High Medium 10 High High High 11 Low Medium Low 12 Low Low Low 13 Medium High High 14 High Low Low 15 High Medium High 16 High High High 17 Low Medium High 18 Low Low Medium 19 Medium Medium Low 20 Medium Medium High
要在上面创建的数据框上为 df2 中的每一行查找行模式,请将以下代码添加到上述代码片段中 -
y1<-sample(c("Low","Medium","High"),20,replace=TRUE)
y2<-sample(c("Low","Medium","High"),20,replace=TRUE)
y3<-sample(c("Low","Medium","High"),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2$Row_Mode<-apply(df2[], 1, function(x) {names(which.max(table(factor(x,unique(x)))))})
df2输出
如果您将上面给出的所有代码片段作为单个程序执行,它将生成以下输出 -
y1 y2 y3 Row_Mode 1 High Low Low Low 2 Low High High High 3 Low Low High Low 4 High Medium Low High 5 High Medium Low High 6 Medium Low Medium Medium 7 Low High Low Low 8 Low High Low Low 9 Medium High Medium Medium 10 High High High High 11 Low Medium Low Low 12 Low Low Low Low 13 Medium High High High 14 High Low Low Low 15 High Medium High High 16 High High High High 17 Low Medium High Low 18 Low Low Medium Low 19 Medium Medium Low Medium 20 Medium Medium High Medium
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP