计算具有字符串值的跨因子变量的行均值

时间:2018-11-13 20:49:07

标签: r average

我有四个由值组成的变量:正确/不正确:

Var1      Var2      Var3      Var4 
Correct   Correct   Correct   Correct
Incorrect Incorrect Incorrect Correct
Incorrect Incorrect Correct   Incorrect
Incorrect Correct   Correct   Correct

我想知道是否有一种简单的方法来计算我可以分配为新变量的每一行的平均分数。不必在数据集中实际将“正确/不正确”替换为1/0。

该分数是为了确保正确性,因此例如row1的分数为1,row4的分数为.75。

1 个答案:

答案 0 :(得分:1)

使用rowMeans

df1$new_Var <- rowMeans(df1 == "Correct")
df1
#       Var1      Var2      Var3      Var4 new_Var
#1   Correct   Correct   Correct   Correct    1.00
#2 Incorrect Incorrect Incorrect   Correct    0.25
#3 Incorrect Incorrect   Correct Incorrect    0.25
#4 Incorrect   Correct   Correct   Correct    0.75

数据

df1 <- read.table(text = "Var1      Var2      Var3      Var4 
Correct   Correct   Correct   Correct
Incorrect Incorrect Incorrect Correct
Incorrect Incorrect Correct   Incorrect
Incorrect Correct   Correct   Correct", stringsAsFactors = FALSE, header = TRUE)