I'd like to use an if else statement to make a new column in my dataframe based on data in another column. I've looked at a number of prior (such as this one and this one), but seem to be doing something wrong as I either get an error or no new column.
I've tried making an ifelse function:
if(x >= 4000)
{print (">4000")
} else if (x >=3000 & x <= 4000)
{print ("3000-4000")
} else if (x >=2000 & x <= 3000)
{print("2000-3000")
} else if (x >=1000 & x <= 2000)
{print("1000-2000")
} else print ("<1000")}
this function works/ran but I can't figure out how to apply it to one column in my dataframe (I've tried this dat$P.bins <- Bins(dat$Pcol)
but get the following error: the condition has length > 1 and only the first element will be used1 ">4000"
I've also tried to run a ifelse statement:
dat$P.bin<- ifelse(P.col>=4000, ">4000",
ifelse(P.col <=4000 & >= 3000, "3000-4000"),
ifelse(P.col<=3000 & >= 2000, "2000-3000"),
ifelse(P.col <=2000 & >=1000, "1000-2000"),
ifelse(P.col <1000, "1000"))
but get this error: Error: unexpected '>=' in:"dat$P.bins <- ifelse(Pcol >=4000, ">4000",felse(Pcol <=4000 & >=". With this statement I'm not sure how to do a range in the ifelse statement.
Any help or guidance would be greatly appreciated!