遍历变量以更改值

时间:2018-08-03 17:53:39

标签: r lapply

我确定这是重复的并且是一个简单的问题。

我有一个带有字符串的数据框。所有值都是字符类。如果行中少于5个字符,我想将所有列中的所有行都转换为NA。我尝试执行以下操作,但不起作用。我想念什么?

mydataset <- tibble(
  A = c("Short answer", "Really long answer really long answer", "N/A"),
  B = c("Short answer", "Really long answer really long answer", "n/K"),
  C = c("Really super long answer I want to keep", ".?", "Short")
)
str(mydataset)

lapply(mydataset, function(x) { nchar(x) < 5 <- NA; x})  # incorrect number of arguments to "<<-"

h / t R lapply convert NA's to 0Delete rows with less than 7 Characters

0 个答案:

没有答案