Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205301

How to fill NA rows by conditions from columns in R

$
0
0

Here is an example:

df<-data.frame(v1=rep(1:2, 4), 
               v2=rep(c("a", "b"), each=4), 
               v3=paste0(rep(1:2, each=4), rep(c("m", "n", "o", "p"), each=2)), 
               v4=c(1,2, NA, NA, 3,4, NA,NA),
               v5=c(5,6, NA, NA, 7,8, NA,NA),
               v6=c(9,10, NA, NA, 11,12, NA,NA))

df
  v1 v2 v3 v4 v5 v6
1  1  a 1m  1  5  9
2  2  a 1m  2  6 10
3  1  a 1n NA NA NA
4  2  a 1n NA NA NA
5  1  b 2o  3  7 11
6  2  b 2o  4  8 12
7  1  b 2p NA NA NA
8  2  b 2p NA NA NA

What I wanted is, if column v1+v2+v3 are same by ignore the last letter of v3, fill the NAs from the rows that are not NA . In this case, row3's NA should be filled by row1 due to same 1a1 by ignoring m. So a desired output would be:

  v1 v2 v3 v4 v5 v6
1  1  a 1m  1  5  9
2  2  a 1m  2  6 10
3  1  a 1n  1  5  9
4  2  a 1n  2  6 10
5  1  b 2o  3  7 11
6  2  b 2o  4  8 12
7  1  b 2p  3  7 11
8  2  b 2p  4  8 12

Viewing all articles
Browse latest Browse all 205301

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>