Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

r - How to remove NA data in only one columns?

I have a file that looks like so:

date       A  B
2014-01-01 2  3
2014-01-02 5  NA
2014-01-03 NA NA
2014-01-04 7  11

If I use newdata <- na.omit(data) where data is the above table loaded via R, then I get only two data points. I get that since it will filter all instances of NA. What I want to do is to filter for each A and B so that I get three data points for A and only two for B. Clearly, my main data set is much larger than that and the numbers are different but neither should not matter.

How can I achieve that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use is.na() on the relevant vector of data you wish to look for and index using the negated result. For exmaple:

R> data[!is.na(data$A), ]
        date A  B
1 2014-01-01 2  3
2 2014-01-02 5 NA
4 2014-01-04 7 11
R> data[!is.na(data$B), ]
        date A  B
1 2014-01-01 2  3
4 2014-01-04 7 11

is.na() returns TRUE for every element that is NA and FALSE otherwise. To index the rows of the data frame, we can use this logical vector, but we want its converse. Hence we use ! to imply the opposite (TRUE becomes FALSE and vice versa).

You can restrict which columns you return by adding an index for the columns after the , in [ , ], e.g.

R> data[!is.na(data$A), 1:2]
        date A
1 2014-01-01 2
2 2014-01-02 5
4 2014-01-04 7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...