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. 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