Hüsamettin Tayşi
Hüsamettin Tayşi

Reputation: 574

Replace each row in a list with another data in r

I want to replace last rows (called "h24") of all elements in a list (org) with another list (ch) which its each elements contain single value. Data structures were given at the end. Also I need to keep "h24" as row name.

Here is my original data (org);

$`01-2020`
    [,1]
m5    NA
m10   NA
m15   NA
m30   NA
h1    NA
h2    NA
h3    NA
h4    NA
h5    NA
h6    NA
h8    NA
h12   NA
h18   NA
h24   NA

$`01-2021`
      [,1]
m5   0.910
m10  1.819
m15  2.729
m30  5.401
h1  10.150
h2  20.300
h3  30.450
h4  31.935
h5  42.085
h6  40.242
h8  41.083
h12 54.158
h18 84.400
h24 87.586

$`01-2022`
      [,1]
m5   0.680
m10  1.360
m15  2.029
m30  3.862
h1   6.501
h2  13.002
h3  16.452
h4  18.895
h5  22.970
h6  27.564
h8  37.271
h12 30.600
h18 52.338
h24 55.374

data to replace

$`01-2020`
[1] 103.03

$`01-2021`
[1] 99

$`01-2022`
[1] 87

Desired output

$`01-2020`
        [,1]
    m5    NA
    m10   NA
    m15   NA
    m30   NA
    h1    NA
    h2    NA
    h3    NA
    h4    NA
    h5    NA
    h6    NA
    h8    NA
    h12   NA
    h18   NA
    h24   103.03
    
    $`01-2021`
          [,1]
    m5   0.910
    m10  1.819
    m15  2.729
    m30  5.401
    h1  10.150
    h2  20.300
    h3  30.450
    h4  31.935
    h5  42.085
    h6  40.242
    h8  41.083
    h12 54.158
    h18 84.400
    h24 99
    
    $`01-2022`
          [,1]
    m5   0.680
    m10  1.360
    m15  2.029
    m30  3.862
    h1   6.501
    h2  13.002
    h3  16.452
    h4  18.895
    h5  22.970
    h6  27.564
    h8  37.271
    h12 30.600
    h18 52.338
    h24 87
 

Data structures

  1. original data

    org<-list(`01-2020` = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
             NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
             NA_real_, NA_real_, NA_real_), .Dim = c(14L, 1L), .Dimnames = list(
                 c("m5", "m10", "m15", "m30", "h1", "h2", "h3", "h4", "h5", 
                 "h6", "h8", "h12", "h18", "h24"), NULL)), `01-2021` = structure(c(0.91, 
             1.819, 2.729, 5.401, 10.15, 20.3, 30.45, 31.935, 42.085, 40.242, 
             41.083, 54.158, 84.4, 87.586), .Dim = c(14L, 1L), .Dimnames = list(
                 c("m5", "m10", "m15", "m30", "h1", "h2", "h3", "h4", "h5", 
                 "h6", "h8", "h12", "h18", "h24"), NULL)), `01-2022` = structure(c(0.68, 
             1.36, 2.029, 3.862, 6.501, 13.002, 16.452, 18.895, 22.97, 27.564, 
             37.271, 30.6, 52.338, 55.374), .Dim = c(14L, 1L), .Dimnames = list(
                 c("m5", "m10", "m15", "m30", "h1", "h2", "h3", "h4", "h5", 
                 "h6", "h8", "h12", "h18", "h24"), NULL)))
    

data to replace

ch<-list(`01-2020` = 103.03, `01-2021` = 87.58, `01-2022` = 55.37)

Upvotes: 1

Views: 40

Answers (1)

akrun
akrun

Reputation: 886948

We can use Map to loop over the corresponding elements of both list and replace the last row (nrow) with the element of 'ch'

org1 <- Map(function(x, y) replace(x, nrow(x), y), org, ch[names(org)])

If it is to replace only when it is a missing value

org1 <- Map(function(x, y) replace(x, seq_len(nrow(x)) == nrow(x) & 
        is.na(x), y), org, ch[names(org)])

Upvotes: 2

Related Questions