答案 0 :(得分:2)
df[] <- lapply(df, function(x) ifelse(x == "correct", 1, 0))
df
x y z
1 1 1 1
2 1 1 0
3 1 1 1
4 0 0 1
5 0 1 0
6 0 0 1
或者仅适用于某些列:
cols <- c("x", "z")
df[cols] <- lapply(df[cols], function(x) as.integer(x == "correct"))
df
x y z
1 1 correct 1
2 1 correct 0
3 1 correct 1
4 0 incorrect 1
5 0 correct 0
6 0 incorrect 1
哪里
df <- data.frame(
x = c("correct", "correct", "correct", "incorrect", "incorrect", "incorrect"),
y = c("correct", "correct", "correct", "incorrect", "correct", "incorrect"),
z = c("correct", "incorrect", "correct", "correct", "incorrect", "correct")
)
答案 1 :(得分:0)
将其转换为逻辑,然后将逻辑转换为数字。 TRUE
始终等于1,FALSE
始终等于0。
a <- c('Correct', 'Incorrect', 'Correct')
sapply(a=='Correct', as.numeric)
[1] 1 0 1
如果要将其应用于数据框。
df <- data.frame(a=c('Correct', 'Incorrect', 'Correct'), b=c('Correct', 'Incorrect', 'Incorrect'))
df
a b
1 Correct Correct
2 Incorrect Incorrect
3 Correct Incorrect
matrix(sapply(df=='Correct', as.numeric), ncol = ncol(df))
[,1] [,2]
[1,] 1 1
[2,] 0 0
[3,] 1 0
答案 2 :(得分:0)
您可以在不指定条件的情况下将列转换为因子,然后转换为数值。
#Create data frame
> df <- data.frame(a = c("correct", "correct", "correct"),
b = c("incorrect", "correct", "incorrect") )
> df
a b
1 correct incorrect
2 correct correct
3 correct incorrect
#Convert character columns to numeric using apply:
> df2 <- as.data.frame(apply(df,2,function(L){as.numeric(as.factor(L))}))
> df2
a b
1 1 2
2 1 1
3 1 2