2017-04-06 55 views
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。任何指導將非常感謝!謝謝:)

回答

0

這裏是一個dplyr解決您的第一個問題:

condition <- "word_2" 
library(dplyr) 
mutate(df, Var = lapply(Var, mutate, N = ifelse(N == condition, "A", N))) 
#       Var 
# 1   word_1, A, word_3 
# 2 word_1, A, word_3, word_4 

一個翻譯基礎R:

"$<-"(df, Var, lapply(df$Var, function(x) 
    "$<-"(x, N, ifelse(x$N == condition, "A", x$N)) 
)) 

因爲你似乎使用data.table,我試圖瓜分一些data.table等價但我對語法不太熟悉,所以它可能不是很習慣:

library(data.table) 
DT <- as.data.table(df) 
DT[, .(Var = list(as.data.table(Var)[, ifelse(N == condition, "A", N)])), by = seq_len(nrow(DT))] 

關於第二個問題,這是一個簡單的替換N == conditiongrepl(condition, N)

mutate(df, Var = lapply(Var, mutate, N = ifelse(grepl("_", N), "B", N)))