Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select data that have complete cases of a certain column?

Tags:

r

I'm trying to get a data frame (just.samples.with.shoulder.values, say) contain only samples that have non-NA values. I've tried to accomplish this using the complete.cases function, but I imagine that I'm doing something wrong syntactically below:

data <- structure(list(Sample = 1:14, Head = c(1L, 0L, NA, 1L, 1L, 1L, 
0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L), Shoulders = c(13L, 14L, NA, 
18L, 10L, 24L, 53L, NA, 86L, 9L, 65L, 87L, 54L, 36L), Knees = c(1L, 
1L, NA, 1L, 1L, 2L, 3L, 2L, 1L, NA, 2L, 3L, 4L, 3L), Toes = c(324L, 
5L, NA, NA, 5L, 67L, 785L, 42562L, 554L, 456L, 7L, NA, 54L, NA
)), .Names = c("Sample", "Head", "Shoulders", "Knees", "Toes"
), class = "data.frame", row.names = c(NA, -14L))

just.samples.with.shoulder.values <- data[complete.cases(data[,"Shoulders"])]
print(just.samples.with.shoulder.values)

I would also be interested to know whether some other route (using subset(), say) is a wiser idea. Thanks so much for the help!

like image 851
Atticus29 Avatar asked Sep 12 '12 17:09

Atticus29


People also ask

How do I select a complete case in R?

Complete cases in R, To eliminate missing values from a vector, matrix, or data frame, use the complete. cases() function in R. The following is the fundamental syntax for this function. In any column in the data frame, remove rows with missing values.

What does complete cases do in R?

complete. cases() function in R Language is used to return a logical vector with cases which are complete, i.e., no missing value.


2 Answers

You can try complete.cases too which will return a logical vector which allow to subset the data by Shoulders

data[complete.cases(data$Shoulders), ] 
#    Sample Head Shoulders Knees Toes
#  1      1    1        13     1  324
#  2      2    0        14     1    5
#  4      4    1        18     1   NA
#  5      5    1        10     1    5
#  6      6    1        24     2   67
#  7      7    0        53     3  785
#  9      9    1        86     1  554
# 10     10    1         9    NA  456
# 11     11    1        65     2    7
# 12     12    1        87     3   NA
# 13     13    0        54     4   54
# 14     14    1        36     3   NA
like image 89
AHegde Avatar answered Sep 20 '22 14:09

AHegde


You could try using is.na:

data[!is.na(data["Shoulders"]),]
   Sample Head Shoulders Knees Toes
1       1    1        13     1  324
2       2    0        14     1    5
4       4    1        18     1   NA
5       5    1        10     1    5
6       6    1        24     2   67
7       7    0        53     3  785
9       9    1        86     1  554
10     10    1         9    NA  456
11     11    1        65     2    7
12     12    1        87     3   NA
13     13    0        54     4   54
14     14    1        36     3   NA
like image 33
James Avatar answered Sep 21 '22 14:09

James