Dear R users, I want to remove zero's or NA values after this model. year value1 value2 1854 0 12 1855 0 13 1866 12 16 1877 11 24 year value1 value2 1 12 12 2 11 13 3 16 4 24 Thank you! -- --- Catalin-Constantin ROIBU Forestry engineer, PhD Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX: +4 0230 52 16 64 silvic.usv.ro [[alternative HTML version deleted]]
na.omit -- View this message in context: http://r.789695.n4.nabble.com/remove-NA-or-0-values-tp4651096p4651097.html Sent from the R help mailing list archive at Nabble.com.
Hello, You can't keep the tabular form and not have the same number of elements in value2, what you can have is a list. (I'm not seeing much sense in it, but if that's wht you want...) dat <- read.table(text = " year value1 value2 1854 0 12 1855 0 13 1866 12 16 1877 11 24 ", header = TRUE) idx <- dat$value1 == 0 | is.na(dat$value1) result <- vector("list", 3) names(result) <- names(dat) result$year <- seq_len(nrow(dat)) result$value1 <- unname(unlist(dat[!idx, 2:3])) result$value2 <- unlist(dat[idx, 3]) result Hope this helps, Rui Barradas Em 28-11-2012 08:53, catalin roibu escreveu:> Dear R users, > I want to remove zero's or NA values after this model. > > year value1 value2 > 1854 0 12 > 1855 0 13 > 1866 12 16 > 1877 11 24 > > year value1 value2 > 1 12 12 > 2 11 13 > 3 16 > 4 24 > > Thank you! > >
Hello, You should keep this in the list, the odds of getting more and better answers are bigger. As for your dataset, it doesn't have the same structure as your previous example. If you want a list with all zeros and NAs removed you can try (assuming it's named 'dat') lapply(dat, function(x) x[x != 0 & !is.na(x)]) Hope this helps, Rui Barradas Em 28-11-2012 15:12, catalin roibu escreveu:> Hello, > Thanks for your help! But the problem still persist. > My data is in the attach. > > Thank you! > > > On 28 November 2012 14:08, Rui Barradas <ruipbarradas at sapo.pt> wrote: > >> Hello, >> >> You can't keep the tabular form and not have the same number of elements >> in value2, what you can have is a list. >> (I'm not seeing much sense in it, but if that's wht you want...) >> >> dat <- read.table(text = " >> >> year value1 value2 >> 1854 0 12 >> 1855 0 13 >> 1866 12 16 >> 1877 11 24 >> ", header = TRUE) >> >> idx <- dat$value1 == 0 | is.na(dat$value1) >> result <- vector("list", 3) >> names(result) <- names(dat) >> result$year <- seq_len(nrow(dat)) >> result$value1 <- unname(unlist(dat[!idx, 2:3])) >> result$value2 <- unlist(dat[idx, 3]) >> result >> >> >> Hope this helps, >> >> Rui Barradas >> Em 28-11-2012 08:53, catalin roibu escreveu: >> >> Dear R users, >>> I want to remove zero's or NA values after this model. >>> >>> year value1 value2 >>> 1854 0 12 >>> 1855 0 13 >>> 1866 12 16 >>> 1877 11 24 >>> >>> year value1 value2 >>> 1 12 12 >>> 2 11 13 >>> 3 16 >>> 4 24 >>> >>> Thank you! >>> >>> >>> >
df <- df[-which(is.na(df$Field)), ] - removing rows with NULL fields df <- df[-which(df$Field == "" ), ] - removing rows with empty fields On Wednesday, November 28, 2012 2:23:00 PM UTC+5:30, catalin roibu wrote:> > Dear R users, > I want to remove zero's or NA values after this model. > > year value1 value2 > 1854 0 12 > 1855 0 13 > 1866 12 16 > 1877 11 24 > > year value1 value2 > 1 12 12 > 2 11 13 > 3 16 > 4 24 > > Thank you! > > > -- > --- > Catalin-Constantin ROIBU > Forestry engineer, PhD > Forestry Faculty of Suceava > Str. Universitatii no. 13, Suceava, 720229, Romania > office phone +4 0230 52 29 78, ext. 531 > mobile phone +4 0745 53 18 01 > +4 0766 71 76 58 > FAX: +4 0230 52 16 64 > silvic.usv.ro > > [[alternative HTML version deleted]] > > ______________________________________________ > R-h... at r-project.org <javascript:> 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. >