here is my sample data
#data
a <- c(1,2,3,4,5,6)
b <- c('x','','x~y~z','z','','x')
#created the dataframe
c<-data.frame(a,b)
#viewing the dataframe
c
#output
a b
1 1 x
2 2
3 3 x~y~z
4 4 z
5 5
6 6 x
My Expected output is
this is what i tried using a function , I tried in creating a new function as below
getC <- function(df, col,y){
if(df[,col]==y){
return(y)
}else if(is.na(df[,col])){
return("")
}else if(strsplit(unlist(T_FRM_G[11,4]),'~')==y){
return(y)
break
}
}
#calling the functions and creating a new column x,y,z and the
functions should split the based on ~ of those respective cells.
c$x <- getC(c,"b","x")
c$y <- getC(c,"b","y")
c$z <- getC(c,"b","z")
the below is the error
> c$x <- getC(c,"b","x")
Warning message:
In if (df[, col] == y) { :
the condition has length > 1 and only the first element will be used
Please help why i am getting that error and what i should do to avoid those errors.
