[R] remove NA or 0 values

Rui Barradas ruipbarradas at sapo.pt
Wed Nov 28 13:08:29 CET 2012


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!
>
>




More information about the R-help mailing list