Skip to content
Prev 247654 / 398506 Next

dataframe: string operations on columns

Hi,

I guess it's not the nicest way to do it, but it should work for you:

#create some sample data
df <- data.frame(a=c("A B", "C D", "A C", "A D", "B D"), 
stringsAsFactors=FALSE)
#split the column by space
df_split <- strsplit(df$a, split=" ")

#place the first element into column a1 and the second into a2
for (i in 1:length(df_split[[1]])){
  df[i+1] <- unlist(lapply(df_split, FUN=function(x) x[i]))
  names(df)[i+1] <- paste("a",i,sep="")
}

I hope people will give you more compact solutions.
HTH,
Ivan



Le 1/18/2011 16:30, boris pezzatti a ?crit :