简体   繁体   中英

mutate a new column using dplyr with values based on multiple conditions; Tried lapply but still not working

I have a data frame with multiple columns in which I want to create a new column with values based on the column status.

I'm new in R, but I think it would be possible to do this.

My str() of the dataframe is:

字符串()

My column status contains a fault code with values like 240:12, 05:03: 90:312 etc. But some codes are not fault codes, just information. So I want to create a new column which states which code is a fault and which not.

I know that codes starting with:

"00","01","02","03","04","05","07","08","09","10","11","12","14","15","16","17","20","21","60","240","600"

are not a fault, the others are a fault code.

The values in Status are character.

My solution would be:

dataframe3 %>% 
  mutate(Status_fault = case_when(startsWith(Status,C("00","01","02",
            "03","04","05","07","08","09","10","11",
            "12","14","15","16","17","20","21","60","240","600"))
   ~ "No fault",
    T ~ "fault"))

But this results in

Error: Problem with mutate() input Status_problem. x object not interpretable as a factor i Input Status_problem is case_when(...).

Anyone an idea to solve this? I have searched on stack overflow everywhere but am looking for so long, I have the feeling I can't think straight anymore...

The question was associated with another question using lapply. So I made a new solution:

dataframe3 %>% 
  mutate(Status_problem = case_when(lapply(c('00','01','02','03','04','05','07','08','09','10','11','12','14','15','16','17','20','21','60','240','600'),starts_with, X = Status)
   ~ "No fault",
    T ~ "fault"))

Unfortunately this results in:

Error: Problem with mutate() input Status_problem. xc("'c("00", "01", "02", "03", "04", "05", "07", "08", "09", "10", ' is not a function, character or symbol", "' "11", "12", "14", "15", "16", "17", "20", "21", "60", "240", ' is not a function, character or symbol", "' "600")' is not a function, character or symbol") i Input Status_problem is case_when(...).

Anyone sees what I'm doing wrong?

Try this:

noFaultCodes = c("00","01","02", "03","04","05","07","08","09","10","11",
                 "12","14","15","16","17","20","21","60","240","600")    
dataframe3 %>% mutate(Status_fault = ifelse(gsub(':.*', '', Status) %in% noFaultCodes,
                                        "No fault", "fault"))

The gsub() trims off everything after : from your Status column. The %in% checks if the trimmed string is in the collection we made called noFaultCodes .

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