用1代替刻度线√

我有一些学生在Excel文件中通过符号√进行检查的数据。我需要在我的栏中将其替换为1(一)。我在此符号出现的列中有50个值。如何使用dplyr执行此操作?或其他一些R函数?

评论
quo_et
quo_et

You could use str_replace from stringr:

library(dplyr)
library(stringr)
data
  A B
1 1 √
2 2 2
3 3 √
4 4 √
5 5 2

data %>%
  mutate(B = str_replace(B,"√","1"))
  A B
1 1 1
2 2 2
3 3 1
4 4 1
5 5 2

You could add as.numeric if you want to also convert to numeric:

data %>%
  mutate(B = as.numeric(str_replace(B,"√","1")))

样本数据

set.seed(3)
data <- data.frame(A= 1:5,B = gsub("1","√",round(runif(5,1,2))))
点赞
评论