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 one with only one as follows: From Name.x Name.y nx1 ny1 nx2 NA NA ny3 NA NA ... To Name nx1 nx2 ny3 NA ... where column Name.x is taken if there is a value, and if not then column Name.y Any help would be appreciated Kind regards, Ivan
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 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 one with only one as follows: > > From > > Name.x Name.y > nx1 ny1 > nx2 NA > NA ny3 > NA NA > ... > > To > > Name > nx1 > nx2 > ny3 > NA > ... > > where column Name.x is taken if there is a value, and if not then column > Name.y > > Any help would be appreciated > > Kind regards, > Ivan > > ______________________________________________ > R-help at 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.
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 go from a dataframe > with two columns to one with only one as follows: > > From > > Name.x Name.y > nx1 ny1 > nx2 NA > NA ny3 > NA NA > ... > > To > > Name > nx1 > nx2 > ny3 > NA > ... > > where column Name.x is taken if there is a value, and if not then > column Name.y > > Any help would be appreciatedI don't know of any special function to do that, but ifelse() can handle it easily: Name <- ifelse(is.na(Name.x), Name.y, Name.x) (If those are columns of a dataframe named df, you'd prefix each column name by df$, or do within(df, Name <- ifelse(is.na(Name.x), Name.y, Name.x)) Duncan Murdoch
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 > "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 one with only one as follows: > > From > > Name.x Name.y > nx1 ny1 > nx2 NA > NA ny3 > NA NA > ... > > To > > Name > nx1 > nx2 > ny3 > NA > ... > > where column Name.x is taken if there is a value, and if not then column > Name.y > > Any help would be appreciated > > Kind regards, > Ivan > > ______________________________________________ > R-help at 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.