註釋

2017-01-16 114 views
0

我有一個字符串每一行中這樣的數據幀:註釋

col_name 
col_string 
It is a rainy day 
Daily exercise 
My name is 
Hello 

我想利用這個規則

day <- c("day", "daily") 
    name <- c("name") 

標註我的數據集,並有一個最終輸出(基於前幾組的第二列):

col_string, col_annotated 
It is a rainy day, day 
Daily exercise, day 
My name is, name 
Hello, NA 

是否可以做到這一點?

回答

0
d <- data.frame(col_string = c('It is a rainy day', 
           'Daily exercise', 
           'My name is', 
           'Hello')) 


d$col_annotated <- ifelse(grepl('day', d$col_string, T) | grepl('daily', d$col_string, T), 'day', 
          ifelse(grepl('name', d$col_string, T), 'name', NA)) 

d 
##   col_string col_annotated 
## 1 It is a rainy day   day 
## 2 Daily exercise   day 
## 3  My name is   name 
## 4    Hello   <NA> 
1
library(dplyr) 

df %>% 
    mutate(col_annotated = case_when(grepl("day", .$col_string, T) ~ "day", 
            grepl("name", .$col_string, T) ~ "name"))