[R] data frame formatting

Jon BR jonsleepy at gmail.com
Tue Aug 18 21:06:36 CEST 2015


Hello all,
    I would like to take a data frame such as the following one:

> df <-
data.frame(id=c("A","A","B","B"),first=c("BX",NA,NA,"LF"),second=c(NA,"TD","BZ",NA),third=c(NA,NA,"RB","BT"),fourth=c("LG","QR",NA,NA))
> df
  id first second third fourth
1  A    BX   <NA>  <NA>     LG
2  A  <NA>     TD  <NA>     QR
3  B  <NA>     BZ    RB   <NA>
4  B    LF   <NA>    BT   <NA>

and merge rows based on the id, such that the value in the column will be
one of four possibilities: if both values in the original df are <NA>, the
new value should also be <NA>.  If there are two non-NA values, then the
new value should read "clash".  Otherwise, the new value should be
whichever value was not <NA>.

An example output from the command would read in df and read out:


  id first second third fourth
1  A    BX   TD  <NA>     clash
2  B    LF   BZ    clash   <NA>


I'd be grateful if someone could point me in the right direction.

Thanks,
Jonathan

	[[alternative HTML version deleted]]



More information about the R-help mailing list