0
我有數據幀的數據幀,看起來像這樣:應用條件的作用是複雜的data.frame中的R
> df
Var
1 word_1, word_2, word_3
2 word_1, word_2, word_3, word_4
> dput(df)
structure(list(df = list(structure(list(N = c("word_1", "word_2", "word_3")),
.Names = "N", row.names = c(NA, -3L), class = "data.frame"), structure(list(N
= c("word_1", "word_2", "word_3", "word_4")),
.Names = "N", row.names = c(NA, -4L), class = "data.frame"))), .Names = "Var",
row.names = c(NA, -2L), class = "data.frame")
我想的函數施加到數據,使得如果一個字一個條件匹配,它被替換。我想是這樣的:
func_1 <- function(dataset, condition){
require(data.table)
setDT(dataset)[, lapply(.SD, function(x) ifelse(x == condition, "A", x))]
}
df <- lapply(df, func_1, condition = "word_2")
但我得到的錯誤:
Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow =
nr, :
'df' must be of a vector type, was 'NULL'
我還需要一個功能極爲相似,不同的是我希望能夠在條件發生更換的話func_1
某處這個詞。例如,func_2
會使包含"_"
的任何單詞被某個字符替換,如B
。任何指導將非常感謝!謝謝:)