[R] how to delete columns with NA values?

Stefan Uhmann stefan.uhmann at googlemail.com
Wed Apr 14 17:14:34 CEST 2010


Hi muting,

# your data
muting <- data.frame(col1 = c(1,1,2,1,2,1), col2=c(NA,1,2,1,2,NA))
# 1. finding rows with NA
is.na(muting)
# 2. counting the NAs per column
colSums(is.na(muting))
# 3. keeping only the ones without NAs
muting[,colSums(is.na(muting)) == 0]

Regards,
Stefan

schrieb muting, Am 14.04.2010 16:56:
>
> Hi everyone:
>
> I have a dataset:
>
> tm1
>       col1 col2
> [1,]    1   NA
> [2,]    1    1
> [3,]    2    2
> [4,]    1    1
> [5,]    2    2
> [6,]    1   NA
>
> I need to delete entire column 2 that has NA in it(not all of them are NAs),
> and the result I want is
>
> tm1
>       col1
> [1,]    1
> [2,]    1
> [3,]    2
> [4,]    1
> [5,]    2
> [6,]    1
>
> what should I do?
> I search a lot, all I found is how to delete column with all NA values..
>
> Thanks a lot
>
> muting
>



More information about the R-help mailing list