HarpoonHarry
HarpoonHarry

Reputation: 215

Replace two cells in a dataframe with another dataframe

Apologies, I imagine this has been asked before but I'm having trouble finding it. Perhaps I'm describing it poorly. I need to overwrite some values in my data frame.

I've got the following input:

input = structure(list(a.Date = c("19-Sep-18", "19-Sep-18", "18-Mar-19", 
"18-Mar-19", "7-Dec-18", "2-Aug-18", "15-Aug-18", "8-Mar-19", 
"21-Dec-18", "29-Jul-19"), Name = c("Frank", "Gene", "Simon", 
"Bob", "Bill", "Tony", "Scott", "Angela", "Millie", "Fred"), 
    a.Result = c(381, 84, 851, 550, 87, 922, 59, 912, 603, 113
    ), Rank = c(1L, 2L, 4L, 3L, 5L, 8L, 6L, 7L, 9L, 10L), b.Result = c(891, 
    525, 795, 697, 351, 724, 341, 615, 395, 229), b.Date = c("19-Sep-18", 
    "19-Sep-18", "18-Mar-19", "18-Mar-19", "7-Dec-18", "2-Aug-18", 
    "15-Aug-18", "8-Mar-19", "21-Dec-18", "29-Jul-19"), c.Result = c(931, 
    462, 323, 936, 996, 26, 93, 820, 468, 265), c.Date = c("19-Sep-18", 
    "19-Sep-18", "18-Mar-19", "18-Mar-19", "7-Dec-18", "2-Aug-18", 
    "15-Aug-18", "8-Mar-19", "21-Dec-18", "29-Jul-19")), class = "data.frame", row.names = c(NA, 
-10L))

and i have these replacement values:

replace = structure(list(X = structure(17786, class = "Date"), Bill = -2081.568737826), row.names = 1L, class = "data.frame")

I'm trying to get the following output:

output = structure(list(a.Date = c("19-Sep-18", "19-Sep-18", "18-Mar-19", 
"18-Mar-19", "7-Dec-18", "2-Aug-18", "15-Aug-18", "8-Mar-19", 
"21-Dec-18", "29-Jul-19"), Name = c("Frank", "Gene", "Simon", 
"Bob", "Bill", "Tony", "Scott", "Angela", "Millie", "Fred"), 
    a.Result = c(381, 84, 851, 550, 87, 922, 59, 912, 603, 113
    ), Rank = c(1L, 2L, 4L, 3L, 5L, 8L, 6L, 7L, 9L, 10L), b.Result = c(891, 
    525, 795, 697, 351, 724, 341, 615, 395, 229), b.Date = c("19-Sep-18", 
    "19-Sep-18", "18-Mar-19", "18-Mar-19", "7-Dec-18", "2-Aug-18", 
    "15-Aug-18", "8-Mar-19", "21-Dec-18", "29-Jul-19"), c.Result = c("931.00", 
    "462.00", "323.00", "936.00", "-2,081.57", "26.00", "93.00", 
    "820.00", "468.00", "265.00"), c.Date = c("19-Sep-18", "19-Sep-18", 
    "18-Mar-19", "18-Mar-19", "12-Sep-18", "2-Aug-18", "15-Aug-18", 
    "8-Mar-19", "21-Dec-18", "29-Jul-19")), class = "data.frame", row.names = c(NA, 
-10L))

where i replace a couple of values with the "replace" dataframe.

I've tried use a match function to locate the cells to be replaced, but I can't get it to work. Can anyone point me in the right direction?

Upvotes: 0

Views: 47

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

We can match the column name in replace with input$Name. Change the format of Date in replace and update the input dataframe.

inds <- match(names(replace)[-1], input$Name)
replace$X <- format(replace$X, "%d-%b-%y")
input[inds, c("c.Date", "c.Result")] <- replace


input
#      a.Date   Name a.Result Rank b.Result    b.Date c.Result    c.Date
#1  19-Sep-18  Frank      381    1      891 19-Sep-18      931 19-Sep-18
#2  19-Sep-18   Gene       84    2      525 19-Sep-18      462 19-Sep-18
#3  18-Mar-19  Simon      851    4      795 18-Mar-19      323 18-Mar-19
#4  18-Mar-19    Bob      550    3      697 18-Mar-19      936 18-Mar-19
#5   7-Dec-18   Bill       87    5      351  7-Dec-18    -2082 12-Sep-18
#6   2-Aug-18   Tony      922    8      724  2-Aug-18       26  2-Aug-18
#7  15-Aug-18  Scott       59    6      341 15-Aug-18       93 15-Aug-18
#8   8-Mar-19 Angela      912    7      615  8-Mar-19      820  8-Mar-19
#9  21-Dec-18 Millie      603    9      395 21-Dec-18      468 21-Dec-18
#10 29-Jul-19   Fred      113   10      229 29-Jul-19      265 29-Jul-19

Upvotes: 2

Related Questions