Re: [R] dataframe: string operations on columns

2011-01-19 Thread Ivan Calandra
Well, my solution with the loop might be slower (even though I don't see any difference with my system, at least with up to 100 lines and 3 strings to separate), but it works whatever the number of strings. But I should have renamed the columns outside of the loop: names(df)[2:3] <- paste("a", 1

Re: [R] dataframe: string operations on columns

2011-01-18 Thread Waclaw Kusnierczyk
On 01/18/2011 07:05 PM, Henrique Dallazuanna wrote: > Or: > > read.table(textConnection(as.matrix(df)), sep = " ") no, that's too simple: you can't use regular expressions. (well, i guess it's enough for the original problem.) vQ > > > On Tue, Jan 18, 2011 at 11:02 PM, Waclaw Kusnierczyk

Re: [R] dataframe: string operations on columns

2011-01-18 Thread Henrique Dallazuanna
Or: read.table(textConnection(as.matrix(df)), sep = " ") On Tue, Jan 18, 2011 at 11:02 PM, Waclaw Kusnierczyk wrote: > Assuming every row is split into exactly two values by whatever string you > choose as split, one fancy exercise in R data structures is > >dfsplit = function(df, split) >

Re: [R] dataframe: string operations on columns

2011-01-18 Thread Waclaw Kusnierczyk
Assuming every row is split into exactly two values by whatever string you choose as split, one fancy exercise in R data structures is dfsplit = function(df, split) as.data.frame( t( structure(dim=c(2, nrow(df)), unlist(

Re: [R] dataframe: string operations on columns

2011-01-18 Thread Niels Richard Hansen
On 2011-01-18 08:14, Ivan Calandra wrote: 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 th

Re: [R] dataframe: string operations on columns

2011-01-18 Thread Peter Ehlers
On 2011-01-18 08:14, Ivan Calandra wrote: 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 th

Re: [R] dataframe: string operations on columns

2011-01-18 Thread Hadley Wickham
> how can I perform a string operation like strsplit(x," ")  on a column of a > dataframe, and put the first or the second item of the split into a new > dataframe column? > (so that on each row it is consistent) Have a look at str_split_fixed in the stringr package. Hadley -- Assistant Profes

Re: [R] dataframe: string operations on columns

2011-01-18 Thread Ivan Calandra
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

[R] dataframe: string operations on columns

2011-01-18 Thread boris pezzatti
Dear all, how can I perform a string operation like strsplit(x," ") on a column of a dataframe, and put the first or the second item of the split into a new dataframe column? (so that on each row it is consistent) Thanks Boris __ R-help@r-project.