I have a dataFrame

sID <- c("a", "1,2,3", "b", "4,5,6")
rID <- c("shr1125", "bwr331", "bwr330", "vjhr1022")

tmp <- data.frame(cbind(sID,rID))

but I need to split tmp$sID into three different columns, filling locations
where tmp$sID has only one value with NA.

I can split tmp$sID by the comma

tmp.1 <- strsplit(tmp$sID, ",")

but I can't figure out how to convert the resulting list into a dataFrame.

Ideally, tmp will become four columns wide, something like

sID.a  sID.b  sID.c  rID
NA     NA     a        shr1125
1        2       3        bwr331
NA     NA     b       bwr330
4        5        6      vjhr1022

Thoughts or suggestions?

I tried

havecomma - grep(',', tmp$sID)

for( i in 1:nrow(tmp)){
  if (!(tmp[i,] %in% havecomma)){
    tmp$sID[i] <- paste(', ,', tmp$sID[i], sep="")
    }
    }

and thought that I might be able to force the list into a dataframe once
each component had three items, but it just seemed to apply the paste()
function to everything which gave me a list with varying numbers of items.

I'm stuck.

Thanks for your help -

SR






Steven H. Ranney

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to