I am trying to assign values to a new column of a Dataframe based on another column "solve_status" column of the same dataframe. all$solved_status is a factor with 3 labels -'ABC, XYZ, MNP'. I have to assign to 0 or 1 based on condition if (ABC) then 1 else 0.
I have the following data
solved_status
1 ABC
2 XYZ
3 ABC
4 MNP
5 XYZ
6 MNP
I have to change it to
solved_status cls
1 ABC 1
2 XYZ 0
3 ABC 1
4 MNP 0
5 XYZ 0
6 MNP 0
pre$cls <- function(x){if(factor(pre$solved_status[x])=="ABC"){ pre$cls[x] = 1} else {pre[x,'cls'] =0}}
An error occurred-
Error in rep(value, length.out = nrows) : attempt to replicate an object of type 'closure'
then I googled and changed it to -
> func <- function(x){if(as.character(pre[x,'solved_status'])=="ABC"){ pre[x,'cls'] = 1} else { pre[x,'cls'] =0} }
> pre$cls = lapply(pre$solved_status,func)
Got an error again -
Error in Summary.factor(2L, na.rm = FALSE) : 'max' not meaningful for factors
I don't know where I am getting wrong. Can someone please rectify?