Reputation: 2770
I have a dataframe with a lot of NAs. When doing exploratory analysis I would prefer to look at just the non-missing columns when I print to the screen.
mtcars[ 1 , 1 ] <- NA
mtcars[ 2 , 2 ] <- NA
I am looking for an efficent way to select all the non-missing columns for a paticular row. For example in this modifed version of mtcars I would like to select columns 2:11 when looking at row one. For row two select c(1, 3:11). I suspect there is an apply statement I can use here but have managed to figure it out.
Upvotes: 1
Views: 277
Reputation: 66834
How about this:
viewRow <- function(x,row) x[row,!is.na(x[row,])]
viewRow(mtcars,1)
cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 6 160 110 3.9 2.62 16.46 0 1 4 4
viewRow(mtcars,2)
mpg disp hp drat wt qsec vs am gear carb
Mazda RX4 Wag 21 160 110 3.9 2.875 17.02 0 1 4 4
Upvotes: 2
Reputation: 2770
Someone gave me code that worked
names(mtcars)[ which(!is.na(mtcars[ 1 , ] )) ]
names(mtcars)[ which(!is.na(mtcars[ 2 , ] )) ]
might be a better way - but this works
Upvotes: 0