如何在 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
廣告