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

Update single row of data frame in R

my dataframe:

Name Value
Setosa 1
Versicolor 2

So first of all, I want to check if an input matches the name in any row.

My solution for the filter so far:

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

# input$df <- Versicolor

import dplyr
df_table <- df_table %>%
      dplyr::filter(grepl(input$df, Name, ignore.case = TRUE))

If there is a match, I’d like to update/overwrite this row with new values, like in the following table:

Name Value
Setosa 1
Versicolor 3

The name stays the same, but only the value changes.

Do you have any advice?

>Solution :

We can use a join

library(dplyr)
df_table %>%
    left_join(input, by = c("Name")) %>%
    mutate(Value = coalesce(Value.x, Value.x), .keep = "unused")

Or with data.table

library(data.table)
setDT(df_table)[input, Value := i.Value, on = .(Name)]
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