r - Function to replace values in column -
i create new column in data.frame following:
data description:
`'data.frame': 20 obs. of 3 variables: $ gvkey : int 1004 1004 1004 1004 1004 1004 1004 1004 1004 1004 ... $ def : int 0 0 0 0 0 0 0 0 0 0 ... $ fittedrobustratio: num 0.549 0.532 0.519 0.539 0.531 ...` function wrote , doesn't work:
fun.mark <- function(x,y){ if (x==0) { y[y>0.60] <- "del" } else (x==1) { y[y<0.45] <- "del2"}} newdataframe <- ddply(shorterdata,~gvkey,transform,fitcorr=fun.mark(def, fittedrobustratio)) so want def column if 0 , fittedrobustratio > 0.60 replace value "del" , if column def 1 (there 0 or 1 values in column) fittedrobustratio column , replace values <0.45 example "del2". thanks.
to nest ifelse commands, ifelse sets out this:
ifelse(definition e.g. a>b, gets "x" if definition met, gets "y" if definition not met) so should work...
data.frame$new.column <- ifelse ( data.frame$def=="0"&data.frame$fittedrobustratio>0.6, "del", ifelse( data.frame$def=="1"&data.frame$fittedrobustratio<0.45, "del2", "none")) i wrong have not provided reproducible dataframe can't test this.
Comments
Post a Comment