[R] remove NA or 0 values

Rui Barradas ruipbarradas at sapo.pt
Wed Nov 28 17:35:55 CET 2012


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




More information about the R-help mailing list