Hi All, I am reading a file as follow, mydat <- read.table(textConnection("Col1 Col2 col3 Z2 NA NA Z3 X1 NA Z4 Y1 W1"),header = TRUE) 1. "NA" are missing should be replace by 0 2. value that are in COl2 and Col3 should be included in col1 before they appear in col2 and col3. So the output data looks like as follow, X1 0 0 Y1 0 0 W1 0 0 Z2 0 0 Z3 X1 0 Z4 Y1 W1 Thank you in advance [[alternative HTML version deleted]]
Hi Val, Try this: preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1], Col2=NA,col3=NA) rbind(preval,mydat) Jim On Sat, Feb 24, 2018 at 3:34 PM, Val <valkremk at gmail.com> wrote:> Hi All, > > I am reading a file as follow, > > mydat <- read.table(textConnection("Col1 Col2 col3 > Z2 NA NA > Z3 X1 NA > Z4 Y1 W1"),header = TRUE) > > 1. "NA" are missing should be replace by 0 > 2. value that are in COl2 and Col3 should be included in col1 before > they appear > in col2 and col3. So the output data looks like as follow, > > X1 0 0 > Y1 0 0 > W1 0 0 > Z2 0 0 > Z3 X1 0 > Z4 Y1 W1 > > Thank you in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Thank you Jim I wanted a final data frame after replacing the NA's to "0" x1 = rbind(unique(preval),mydat) x2 <- x1[is.na(x1)] <- 0 x2 but I got this, [1] 0 why I am getting this? On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Val, > Try this: > > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1], > Col2=NA,col3=NA) > rbind(preval,mydat) > > Jim > > On Sat, Feb 24, 2018 at 3:34 PM, Val <valkremk at gmail.com> wrote: > > Hi All, > > > > I am reading a file as follow, > > > > mydat <- read.table(textConnection("Col1 Col2 col3 > > Z2 NA NA > > Z3 X1 NA > > Z4 Y1 W1"),header = TRUE) > > > > 1. "NA" are missing should be replace by 0 > > 2. value that are in COl2 and Col3 should be included in col1 before > > they appear > > in col2 and col3. So the output data looks like as follow, > > > > X1 0 0 > > Y1 0 0 > > W1 0 0 > > Z2 0 0 > > Z3 X1 0 > > Z4 Y1 W1 > > > > Thank you in advance > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. >[[alternative HTML version deleted]]