r - Inserting NA rows when missing data -
i have data set missing values in sequence:
seq<-c(1,2,3,4,6,7,10,11,12,18,19,20) data<-c(3,4,5,4,3,2,1,2,3,5,4,3) df<-data.frame(seq, data)
i'd add rows data set approximating have missing values, , filling in data na. time have gap larger 2, add na row (or multiple rows if gap large). result this:
newseq<-c(1,2,3,4,6,7,8.5,10,11,12,14,16,18,19,20) newdata<-c(3,4,5,4,3,2,na,1,2,3,na,na,18,19,20) newdf<-data.frame(newseq,newdata)
so ignore when gap < 2, add na row anytime there gap > 2. if there still > 2 gap after adding na row, add until gap filled.
not elegant, how it:
seq<-c(1,2,3,4,6,7,10,11,12,18,19,20) data<-c(3,4,5,4,3,2,1,2,3,5,4,3) df<-data.frame(seq, data) first <- df$seq second <- df$data for(i in length(first):2) { gap <- first[i] - first[i - 1] if(gap > 2) { steps <- ifelse(gap %% 2 == 1, gap %/% 2, (gap %/% 2) -1) new_values_gap <- gap / (steps + 1) new_values <- vector('numeric') for(j in 1:steps) { new_values <- c(new_values, first[i - 1] + j * new_values_gap) } first <- c(first[1:i - 1], new_values, first[i:length(first)]) second <- c(second[1:i - 1], rep(na, length(new_values)), second[i:length(second)]) } } newdf <- data.frame(newseq = first, newdata = second) > newdf ## newseq newdata ## 1 1.0 3 ## 2 2.0 4 ## 3 3.0 5 ## 4 4.0 4 ## 5 6.0 3 ## 6 7.0 2 ## 7 8.5 na ## 8 10.0 1 ## 9 11.0 2 ## 10 12.0 3 ## 11 14.0 na ## 12 16.0 na ## 13 18.0 5 ## 14 19.0 4 ## 15 20.0 3
Comments
Post a Comment