How to send arguments into transform function in R -
i trying create following function:
mysplit_col <- function(df, colname, sepstr=':', firstname='part1', secondname='part2'){ df_split<- strsplit(as.character(df[,colname]), split=sepstr) transform(df, firstname= sapply(df_split, "[[", 1),secondname= sapply(df_split, "[[", 2)) } > mydf name position 1 hla 1:1-15 2 hla 1:2-16 3 hla 1:3-17 > dput(mydf) structure(list(name = structure(c(1l, 1l, 1l), .label = "hla", class = "factor"), position = structure(1:3, .label = c("1:1-15", "1:2-16", "1:3-17"), class = "factor")), .names = c("name", "position" ), class = "data.frame", row.names = c("1", "2", "3")) > mysplit_col_(mydf, 'position') name position firstname secondname 1 hla 1:1-15 1 1-15 2 hla 1:2-16 1 2-16 3 hla 1:3-17 1 3-17
but in output new columns named 'firstname' , 'secondname' , not 'part1' , 'part2'
example data from: https://stats.stackexchange.com/questions/6806/splitting-a-numeric-column-for-a-dataframe
how can correct problem? help.
Comments
Post a Comment