Fill in the same value for each row by group in R

BIN

The last time I asked the question about fill in the same value for each row by group in R, now I deal with exact the same problem but there are some missing value NA. Here is data,blank"" means that person doesn't exposed in that window, NA treats as missing, 1st means the person exposed in the first window..

ID <- c(1,1,2,2,2,3,3,4,4,4)
x <- c("1st","","1st","1st","","",NA,"1st",NA,"1st")
y <- c("2nd","2nd","","","","2nd","2nd","","",NA)
z <- c("","","3rd","3rd","",NA,"3rd","",NA,"")
m <- c(10:19)
n <- c(20:29)
df <- data.frame(ID,x,y,z,m,n)
library(data.table)
setDT(df)[, c("x1", "y1", "z1") := lapply(.SD, function(x) x[which.max(x !=   "")]), by = ID]

I got the output, it's pretty much the one I want except NA

    ID   x   y   z  m  n  x1  y1  z1
 1:  1 1st 2nd     10 20 1st 2nd    
 2:  1     2nd     11 21 1st 2nd    
 3:  2 1st     3rd 12 22 1st     3rd
 4:  2 1st     3rd 13 23 1st     3rd
 5:  2             14 24 1st     3rd
 6:  3     2nd  NA 15 25     2nd 3rd
 7:  3  NA 2nd 3rd 16 26     2nd 3rd
 8:  4 1st         17 27 1st        
 9:  4  NA      NA 18 28 1st        
10:  4 1st  NA     19 29 1st 

You can see row 6 and 7, ID is 3, it's supposed to fill x1 = NA, row 8,9,10, ID is 4, y1 and z1 will be NA, here is output I want

    ID   x   y   z  m  n  x1  y1  z1
 1:  1 1st 2nd     10 20 1st 2nd    
 2:  1     2nd     11 21 1st 2nd    
 3:  2 1st     3rd 12 22 1st     3rd
 4:  2 1st     3rd 13 23 1st     3rd
 5:  2             14 24 1st     3rd
 6:  3     2nd  NA 15 25 NA   2nd 3rd
 7:  3  NA 2nd 3rd 16 26 NA   2nd 3rd
 8:  4 1st         17 27 1st  NA  NA     
 9:  4  NA      NA 18 28 1st  NA  NA     
10:  4 1st  NA     19 29 1st  NA  NA
Psidom

How about recoding condition for NA to be 0.5 which will prioritize NA over empty string but less than other strings:

df[, c("x1", "y1", "z1") := lapply(.SD, function(x) x[which.max(ifelse(is.na(x), 0.5, x != ""))]), by = ID]

df
#    ID   x   y   z  m  n  x1  y1  z1
# 1:  1 1st 2nd     10 20 1st 2nd    
# 2:  1     2nd     11 21 1st 2nd    
# 3:  2 1st     3rd 12 22 1st     3rd
# 4:  2 1st     3rd 13 23 1st     3rd
# 5:  2             14 24 1st     3rd
# 6:  3     2nd  NA 15 25  NA 2nd 3rd
# 7:  3  NA 2nd 3rd 16 26  NA 2nd 3rd
# 8:  4 1st         17 27 1st  NA  NA
# 9:  4  NA      NA 18 28 1st  NA  NA
#10:  4 1st  NA     19 29 1st  NA  NA

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sum of group but keep the same value for each row in r

From Dev

Replace NA with values in another row of same column for each group in r

From Dev

Replace NA with values in another row of same column for each group in r

From Dev

How to group in a row values of the same value in a column with R?

From Dev

How to group in a row values of the same value in a column with R?

From Dev

fill in NA based on the last non-NA value for each group in R

From Dev

Get each row's value and that of the group in dplyr

From Java

How to select the row with the maximum value in each group

From Dev

Get each row's value and that of the group in dplyr

From Dev

R fill in NA with previous row value with condition

From Dev

select row with the same value with limit for each of them

From Dev

R delete last row in dataframe for each group

From Dev

How to fill in NaN values within the same column, with a minimal value of a group

From Dev

How to fill NaN with a value in same row of the data frame using Pandas

From Dev

Fill in rows to get the same amount in each group after group by. Pandas

From Dev

R: row number of the minimum value in each column

From Dev

dropping current row if the value of current row is the same as previous row in R

From Dev

With Pandas in Python, select the highest value row for each group

From Java

Get row with max value from each group based on multiple column

From Dev

How to get column value from previous row for each group?

From Dev

SQL Query to select each row with max value per group

From Dev

With Pandas in Python, select the highest value row for each group

From Dev

How to group by looking at previous and next value in each row

From Dev

MySQL: GROUP BY and fill empty cells with value from next row from this group

From Dev

First row for each group

From Dev

mongodb how to get max value of each "group with the same key"

From Dev

Fill empty values from a row with the value of next column on the same row on csv file with pandas

From Dev

In MySQL, set value in each row to a DATEDIFF computation on the same rows

From Dev

PHP SQL display list for each row with same value

Related Related

  1. 1

    Sum of group but keep the same value for each row in r

  2. 2

    Replace NA with values in another row of same column for each group in r

  3. 3

    Replace NA with values in another row of same column for each group in r

  4. 4

    How to group in a row values of the same value in a column with R?

  5. 5

    How to group in a row values of the same value in a column with R?

  6. 6

    fill in NA based on the last non-NA value for each group in R

  7. 7

    Get each row's value and that of the group in dplyr

  8. 8

    How to select the row with the maximum value in each group

  9. 9

    Get each row's value and that of the group in dplyr

  10. 10

    R fill in NA with previous row value with condition

  11. 11

    select row with the same value with limit for each of them

  12. 12

    R delete last row in dataframe for each group

  13. 13

    How to fill in NaN values within the same column, with a minimal value of a group

  14. 14

    How to fill NaN with a value in same row of the data frame using Pandas

  15. 15

    Fill in rows to get the same amount in each group after group by. Pandas

  16. 16

    R: row number of the minimum value in each column

  17. 17

    dropping current row if the value of current row is the same as previous row in R

  18. 18

    With Pandas in Python, select the highest value row for each group

  19. 19

    Get row with max value from each group based on multiple column

  20. 20

    How to get column value from previous row for each group?

  21. 21

    SQL Query to select each row with max value per group

  22. 22

    With Pandas in Python, select the highest value row for each group

  23. 23

    How to group by looking at previous and next value in each row

  24. 24

    MySQL: GROUP BY and fill empty cells with value from next row from this group

  25. 25

    First row for each group

  26. 26

    mongodb how to get max value of each "group with the same key"

  27. 27

    Fill empty values from a row with the value of next column on the same row on csv file with pandas

  28. 28

    In MySQL, set value in each row to a DATEDIFF computation on the same rows

  29. 29

    PHP SQL display list for each row with same value

HotTag

Archive