Try to search your question here, if you can't find : Ask Any Question Now ?

grouped dataframe expansion using dplyr in R

HomeCategory: stackoverflowgrouped dataframe expansion using dplyr in R
Avatarkundan asked 1 month ago

I have the following dataframe in R

DF2<-data.frame("A"= c(1,2,3,4,5), "A_1"= c(1,2,3,4,5), "B"= c(1,2,10,NA,NA), "B_1"=c(2,3,10,NA, NA), "ID"=c("A", "A", "A","A","A"))

I have tried to expand the dataframe using the code

 library(dplyr)
 library(tidyr)
 df2=  DF2 %>%
 mutate(E=pmax(A,B,na.rm = TRUE))%>%
 complete(E = seq(pmax(E)))

The column E should consist of the range of values in A and B. A_1, B_1 values should align with the corresponding values of column E

I am getting the following output

      E     A    A_1     B    B_1 ID   

      1     1     1      1     2   A    
      2     2     2      2     3   A    
      3    NA    NA      NA    NA NA   
      4     4     4      NA    NA  A    
      5     5     5      NA    NA  A  

However, I expect the following output

     E ID  A A_1  B B_1
 1   1  A  1   1  1   2
 2   2  A  2   2  2   3
 3   3  A  3   3 NA  NA
 4   4  A  4   4 NA  NA
 5   5  A  5   5 NA  NA
 6   6  A NA  NA NA  NA
 7   7  A NA  NA NA  NA
 8   8  A NA  NA NA  NA
 9   9  A NA  NA NA  NA
 10 10  A NA  NA 10  10

I request someone to help here. The final output should match A_1 value with E column value such that E value should equal A value, else it should return zero. ID is the grouping variable

1 Answers
Best Answer
AvatarMannu answered 1 month ago
Your Answer

13 + 19 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com