简体   繁体   中英

Adding column values based on initial character strings in another column

I'm trying to add "YES/NO" character values to a column based on wether there is a specific character value in another column.

Here is an example :

     V2.x          Clitic
1    can could     NA
2    d should      NA

If the first column starts with ^d or ^ll in example$V2.x , the value in example$Clitic should be YES; if not, it should be NO.

So in the df above example[1,2] should be NO and example[2,2] should be YES.

Looking to automate this on a dataset of several hundred rows and a dozen columns. not sure how to do it, although grepl() seems useful. Would appreciate your help.

Structure:

structure(list(V2.x = structure(c(1L, 19L), .Label = c("can could", 
"can cud", "can may", "can might", "can should", "can will", 
"can would", "could can", "could may", "could might", "could should", 
"could used to", "could will", "d can", "d could", "d may", "d might", 
"d must", "d should", "d used to", "d will", "have to should", 
"have to will", "ll can", "ll could", "ll may", "ll might", "ll must", 
"ll shall", "ll should", "ll used to", "ll would", "may can", 
"may might", "may must", "may shall", "may should", "may used to", 
"may will", "may would", "might can", "might could", "might may", 
"might must", "might shall", "might should", "might will", "might would", 
"might wud", "must can", "must will", "must would", "shall can", 
"shall will", "should can", "should could", "should may", "should might", 
"should must", "should will", "should would", "used to could", 
"will can", "will could", "will kin", "will may", "will might", 
"will must", "will shall", "will should", "will would", "would can", 
"would could", "would may", "would might", "would must", "would should", 
"would will"), class = "factor"), Clitic = c(NA, NA)), row.names = 1:2, class = "data.frame")

You already have the regex to use in grepl which returns logical values.

grepl('^(d|ll)', example$V2.x)
#[1] FALSE  TRUE

To get "Yes"/"No" values plug it in ifelse :

example$Clitic <- ifelse(grepl('^(d|ll)', example$V2.x), 'Yes', 'No')
#Without ifelse
#example$Clitic <- c('No', 'Yes')[grepl('^(d|ll)', example$V2.x) + 1]
example

#       V2.x Clitic
#1 can could     No
#2  d should    Yes

We can use str_detect

library(stringr)
library(dplyr)
example %>%
   mutate(Clitic = case_when(str_detect(V2.x, "^(d|ll)") ~ "Yes", TRUE ~ "No"))    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM