Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to edit a list in a dataframe

I have added a list to the dataframe as shown below

new_df <- structure(list(ID = list(ID = "21", ID = "4"), 
               MET = structure(list(c("A", "B"), c("C", "D")), .Names = c("","")), 
               REGN = structure(list(c("ALL", "US"), "ALL"), .Names = c("", "")), 
               YEAR = list(YEAR = "2020", YEAR = "2020"), 
               WEEK = list(WEEK = c("12", "13", "14"), WEEK = "16"), 
               ANN = list(ANN = "Seller",ANN = "Rise")), row.names = 1:2, class = "data.frame")


new_df
  ID  MET    REGN YEAR       WEEK    ANN
1 21 A, B ALL, US 2020 12, 13, 14 Seller
2  4 C, D     ALL 2020         16   Rise

Suppose if i want to edit the rows here. For example, i need to edit 1 row and REGN column so

new_df[1,"REGN"] <- list(c(New "ALL", "New US"))
Warning message:
In `[<-.data.frame`(`*tmp*`, 1, "REGN", value = list(c("ALL", "USNew" :
  replacement element 1 has 2 rows to replace 1 rows

Expected output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

new_df
  ID  MET            REGN    YEAR       WEEK    ANN
1 21 A, B New ALL, New US    2020 12, 13, 14 Seller
2  4 C, D              ALL   2020         16   Rise

>Solution :

You can use $ to access the column, then select the row with [].

new_df$REGN[1] <- list(c("New ALL", "New US"))

Output

  ID  MET            REGN YEAR       WEEK    ANN
1 21 A, B New ALL, New US 2020 12, 13, 14 Seller
2  4 C, D             ALL 2020         16   Rise
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading