2017-07-24 96 views
-3

刪除特定的細胞我有一個數據集,如:從數據集R中

A    B        C 
    hello   Radiation therapy    NA 
    Hello1   hello2 for neurology   hello3 radiation 

和多行。

現在我打算刪除「for」之後的所有文本,比如「neurology」以及所有包含「radiation」的文本。所以我期待的輸出爲:

A    B       C 
    hello   therapy      NA 
    Hello1   hello2      hello3 

回答

0

實例數據框:

df <- data.frame(B = c("Radiation therapy", "hello2 for neurology")) 

然後將代碼SUB OUT字符串你從數據框的B列想:

df$B <- gsub("Radiation | for.*", "", df$B) 
0

請嘗試以下操作。

dat <- 
structure(list(A = c("hello", "Hello1"), B = c("Radiation therapy", 
"hello2 for neurology"), C = c(NA, "hello3 radiation")), .Names = c("A", 
"B", "C"), row.names = c(NA, -2L), class = "data.frame") 

dat[] <- lapply(dat, function(x) gsub("radiation|for.*", "", x, ignore.case = TRUE)) 
dat 
     A  B  C 
1 hello therapy <NA> 
2 Hello1 hello2 hello3