1
votes

I have a dataset (data1.csv) that contains some missing data (missing at random), I am creating a subset from this dataset (d1) such that only complete observations are retained in d2. I am using the ftable , as.data.frame function and creating a column p that represent the percentage of each combination.

 d1 = read.csv("C:/Users/....../Data1.csv",header=T)

   y x1 x2 x3
1  1  2  1 NA
2  0  2 NA  0
3  1 NA  1  1
4  0  3  1  0
5  0  2  0 NA
6  0  1  0  1
7  1 NA NA  1
8  1  3  0  1
9  1  2  0  0
10 0  3  0 NA
11 1 NA  0  1
12 1  3 NA  1
13 0  3  1 NA
14 0 NA  1  0
15 1  1  0 NA
16 0  1 NA  1
17 1 NA  0 NA
18 0  3  1  1
19 0  1 NA  0
20 0 NA  0  1

 d2= d1[complete.cases(d1),]
 d2

   y x1 x2 x3
4  0  3  1  0
6  0  1  0  1
8  1  3  0  1
9  1  2  0  0
18 0  3  1  1


 d3= ftable(d2)
 d4=as.data.frame(d3)
 d4


 y x1 x2 x3 Freq   p
 1  0  1  0  0    0 0.0
 2  1  1  0  0    0 0.0
 3  0  2  0  0    0 0.0
 4  1  2  0  0    1 0.2
 5  0  3  0  0    0 0.0
 6  1  3  0  0    0 0.0
 7  0  1  1  0    0 0.0
 8  1  1  1  0    0 0.0
 9  0  2  1  0    0 0.0
 10 1  2  1  0    0 0.0
 11 0  3  1  0    1 0.2
 12 1  3  1  0    0 0.0
 13 0  1  0  1    1 0.2
 14 1  1  0  1    0 0.0
 15 0  2  0  1    0 0.0
 16 1  2  0  1    0 0.0
 17 0  3  0  1    0 0.0
 18 1  3  0  1    1 0.2
 19 0  1  1  1    0 0.0
 20 1  1  1  1    0 0.0
 21 0  2  1  1    0 0.0
 22 1  2  1  1    0 0.0
 23 0  3  1  1    1 0.2
 24 1  3  1  1    0 0.0

The function ftable , as.data.frame works fine but the problem is I still see ALL these observations in the results (d4) which dosent make sense. I thought I got rid of this when i did complete.cases(d1)

So need help getting rid of missing values and doing frequency table only for these complete observations

   y x1 x2 x3
4  0  3  1  0
6  0  1  0  1
8  1  3  0  1
9  1  2  0  0
18 0  3  1  1
2
Are the issing values perhaps called '. ' (trailing space) or some other syntactic horror? btw you can also hand in an array of possible missing value symbols to read.csv with the na.strings parameter. - conjugateprior
Please provide a small example of the data. - Rich Scriven

2 Answers

2
votes

Look up ?complete.cases(). It will do what you're looking for; you can specify missing value and what columns you want to do it for.

0
votes

This you sample input data

d1<-structure(list(y = c(1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 
1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L), x1 = c(2L, 2L, NA, 3L, 
2L, 1L, NA, 3L, 2L, 3L, NA, 3L, 3L, NA, 1L, 1L, NA, 3L, 1L, NA
), x2 = c(1L, NA, 1L, 1L, 0L, 0L, NA, 0L, 0L, 0L, 0L, NA, 1L, 
1L, 0L, NA, 0L, 1L, NA, 0L), x3 = c(NA, 0L, 1L, 0L, NA, 1L, 1L, 
1L, 0L, NA, 1L, 1L, NA, 0L, NA, 1L, NA, 1L, 0L, 1L)), .Names = c("y", 
"x1", "x2", "x3"), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16", "17", "18", "19", "20"))

These commands

d2 <- d1[complete.cases(d1),]
d3 <- ftable(d2)
d4 <- as.data.frame(d3)
d4

return the folowing table.

   y x1 x2 x3 Freq
1  0  1  0  0    0
2  1  1  0  0    0
3  0  2  0  0    0
4  1  2  0  0    1
5  0  3  0  0    0
6  1  3  0  0    0
7  0  1  1  0    0
8  1  1  1  0    0
9  0  2  1  0    0
10 1  2  1  0    0
11 0  3  1  0    1
12 1  3  1  0    0
13 0  1  0  1    1
14 1  1  0  1    0
15 0  2  0  1    0
16 1  2  0  1    0
17 0  3  0  1    0
18 1  3  0  1    1
19 0  1  1  1    0
20 1  1  1  1    0
21 0  2  1  1    0
22 1  2  1  1    0
23 0  3  1  1    1
24 1  3  1  1    0

There aren't exactly the original rows. These are all the possible combinations of y, x1, x2, and x3 in your complete cases. If you only want those that actually had observations, you could do

 subset(d4, Freq>0)

but this basically gets you back to d2

4  1  2  0  0    1
11 0  3  1  0    1
13 0  1  0  1    1
18 1  3  0  1    1
23 0  3  1  1    1

It seems unclear what you expected ftable to do or how you expected the result to be different than d2 or where your p column came from. The code is working exactly as I expect so I'm not sure what you thought the result would be.