I have the data frame
df=data.frame(x=rnorm(8),y=runif(8),longstring=c("foo_100_Case1","foo_125_Case1","bar_100_Case1","bar_125_Case1","foo_100_Case2","foo_125_Case2","bar_100_Case2","bar_125_Case2"),stringsAsFactors = F)
I need to split the last column in three columns, with delimiter "_". I've been doing the following:
a=matrix(unlist(strsplit(df$longstring,"_",fixed=T)),8,3,byrow = T)
df$type=a[,1]
df$point=a[,2]
df$case=a[,3]
But I wonder if there's an easier way: the combination of strsplit
and unlist
is particularly awkward, and it doesn't make the code very readable.