Tuesday, 15 March 2011

r - HOW To apply split string to vecotr -



r - HOW To apply split string to vecotr -

i want split based on :: doesnt work entire vector

str<- c("bob::jhh","jjj::dfff") data<- as.data.frame(x) info data$new.col <-strsplit(as.character(b[,1]),"::")[[1]][2] info

output

info new.col 1 bob::jhh jhh 2 jjj::dfff jhh #<<--this should dfff not jhhh.

any thought how create homecoming jhh dfff instead of jhh jhh?

sapply can used here:

str<- c("bob::jhh","jjj::dfff") ss = strsplit(str, split="::") sapply(ss, function(x) x[2]) [1] "jhh" "dfff"

it can set in dataframe:

nn = sapply(ss, function(x) x[2]) dataf$new = nn dataf str new 1 bob::jhh jhh 2 jjj::dfff dfff

r

No comments:

Post a Comment