[R] coalesce columns within a data frame

2008-10-22 Thread Ivan Alves
Dear all, I searched the mail archives and the R site and found no guidance (tried merge, cbind and terms like coalesce with no success). There surely is a way to coalesce (like in SQL) columns in a dataframe, right? For example, I would like to go from a dataframe with two columns to

Re: [R] coalesce columns within a data frame

2008-10-22 Thread Erik Iverson
Not tested, but for data.frame 'df', try df$coal - ifelse(!is.na(df$Name.x), df$Name.x, df$Name.y) But see the help page for 'ifelse' regarding issues with classes, and the myriad R-help posts on the 'ifelse' function. Erik Ivan Alves wrote: Dear all, I searched the mail archives and the

Re: [R] coalesce columns within a data frame

2008-10-22 Thread Duncan Murdoch
On 10/22/2008 11:21 AM, Ivan Alves wrote: Dear all, I searched the mail archives and the R site and found no guidance (tried merge, cbind and terms like coalesce with no success). There surely is a way to coalesce (like in SQL) columns in a dataframe, right? For example, I would like to

Re: [R] coalesce columns within a data frame

2008-10-22 Thread Ivan Alves
Dear all, Thanks for all the replies. I get something with Duncan's code (slightly more compact than the other two), but of class integer, whereas the two inputs are class factor. Clearly the name information is lost. I did not see anything on this in the help page for ifelse. On this

Re: [R] coalesce columns within a data frame

2008-10-22 Thread Duncan Murdoch
On 10/22/2008 12:09 PM, Ivan Alves wrote: Dear all, Thanks for all the replies. I get something with Duncan's code (slightly more compact than the other two), but of class integer, whereas the two inputs are class factor. Clearly the name information is lost. I did not see anything on

Re: [R] coalesce columns within a data frame

2008-10-22 Thread Julian Burgos
You could do something like this: Name.x=c('nx1','nx2',NA,NA) Name.y=c('ny1','NA','ny3',NA) Name=Name.x Name[is.na(Name.x)]=Name.y[is.na(Name.x)] Name [1] nx1 nx2 ny3 NA Julian Ivan Alves wrote: Dear all, I searched the mail archives and the R site and found no guidance (tried

Re: [R] coalesce columns within a data frame

2008-10-22 Thread Ivan Alves
Many thanks to all for their help. Factors are indeed very tricky and sided on the conversion to character. Kind regards, Ivan On 22 Oct 2008, at 19:01, Duncan Murdoch wrote: On 10/22/2008 12:09 PM, Ivan Alves wrote: Dear all, Thanks for all the replies. I get something with Duncan's code