Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

r - 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:

str()

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. x c("'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?

question from:https://stackoverflow.com/questions/65837372/mutate-a-new-column-using-dplyr-with-values-based-on-multiple-conditions-tried

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...