Removing gaps in data frame preserving pairs (R) -
i have basic question in r can't seem sorted. have dataframe looks this:
date x y 1 01/01/2003 00:00 17.04783 na 2 02/01/2003 00:00 14.84500 10.117042 3 03/01/2003 00:00 12.23636 na 4 04/01/2003 00:00 12.62381 na 5 05/01/2003 00:00 na 4.516619 6 06/01/2003 00:00 12.93333 na
i'm interested in cases there both x value , y value, i.e. in above data im interested in row 2.
how create new data frame cases i'm interested in? need preserve date structure too, ideal data this:
date x y 1 01/01/2003 00:00 na na 2 02/01/2003 00:00 14.84500 10.117042 3 03/01/2003 00:00 na na 4 04/01/2003 00:00 na na 5 05/01/2003 00:00 na na 6 06/01/2003 00:00 na na
thanks!
pick out rows want keep values complete.cases()
; set date in other rows na
.
print(df) # date result result2 #1 2015-01-03 0.4179292 na #2 2015-01-05 0.5830931 na #3 2015-01-09 0.9207914 0.3846393 #4 2015-01-13 na 0.4652525 #5 2015-01-17 0.1167908 0.6160894 #6 2015-01-23 0.7866672 0.8541210 #7 2015-01-27 0.8721665 0.8580503 #8 2015-01-29 na na #9 2015-02-01 na 0.4687221 #10 2015-02-06 na 0.2769060 #11 2015-02-11 0.6086726 0.3619493 #12 2015-02-17 0.7421181 na df[!complete.cases(df), -1] <- na print(df) # date result result2 #1 2015-01-03 na na #2 2015-01-05 na na #3 2015-01-09 0.9207914 0.3846393 #4 2015-01-13 na na #5 2015-01-17 0.1167908 0.6160894 #6 2015-01-23 0.7866672 0.8541210 #7 2015-01-27 0.8721665 0.8580503 #8 2015-01-29 na na #9 2015-02-01 na na #10 2015-02-06 na na #11 2015-02-11 0.6086726 0.3619493 #12 2015-02-17 na na
Comments
Post a Comment