如何在R数据帧中用数字替换字符值?

时间:2018-08-14 15:45:54

标签: r replace

我有一个很少有包含两个值正确/错误的变量的数据集 我想将所有正确的替换为1,将不正确的替换为零。

变量的数据类型是字符。我尝试了gsub并替换了没有用的东西。谁能帮我这个。

3 个答案:

答案 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