-3

I have two dataframes.
The First one is the original dataframe.
The second one is a changed dataframe (this data column could be changed by user).

I would like to update the df1 dataframe with df2 (changed data).
Please refer to the expected result e.g1, e.g2

I really appreciate anyone's help.

# data frame 1 (orignal datas)
age <- c(24, 28, 31, 25, 27, 22, 29)

gender <- c("male", "female", "female", "male", "female", "male", "female")
score <- c(90, 80, 85, 75, 95, 80, 70)

df1 <- data.frame(age, gender, score, stringsAsFactors = FALSE)
df1

> df1
  age    gender score
1  24   male    90
2  28 female    80
3  31 female    85
4  25   male    75
5  27 female    95
6  22   male    80
7  29 female    70

# data frame2 (other data frame) - column could be changed by user
id <- c("1", "2")
ch_age <- c(50, 40)
ch_score <- c(80, 80)

df2 <- data.frame(id, ch_age, ch_score, stringsAsFactors = FALSE)
df2

> df2
  ch_id ch_age ch_score
1     1     50       80


exepcted result

e.g1 
  age    gender score
1  50   male    80 --> applied df2 dataframe this row
2  28 female    80
3  31 female    85
4  25   male    75
5  27 female    95
6  22   male    80
7  29 female    70

e.g2
  age    gender score
1  24   male    90
2  28 female    80
3  50 female    80 --> applied df2 dataframe this row
4  25   male    75
5  27 female    95
6  22   male    80
7  29 female    70
bwjang
  • 5
  • 6
  • Please make a clear example what you want to accomplish (logical steps) https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – JAdel Feb 22 '22 at 13:36
  • Why does df1 not have an id column? How should cases be identified between data frames? – JBGruber Feb 22 '22 at 13:38
  • @JAdel I`ll check it thanks for your time. – bwjang Feb 22 '22 at 13:41
  • @JBGruber That is my problem. when i check the message in debug mode. I can`t see the "ID" T.T – bwjang Feb 22 '22 at 13:43

1 Answers1

1
colnames(df2) <- c("sex", "age", "score") 
df2 <- df2[c("age","sex","score")]
for (i in 1:nrow(df2)){
  if(df2[i,2] ==1 ){
    df2[i,2] <- "male"
  }else{
    df2[i,2] <- "female"
  }
}
rbind(df1, df2)

After that you can do operations on rows.

Esad
  • 61
  • 5
  • Thanks for your feed back. I`ll If I changed other row How can I modify it ? Because I need to control the row position. Your logic is great but, you changed df2 dataframe first. in my real data. I`m not sure User will add everything (Not male / female T.T) – bwjang Feb 22 '22 at 14:15
  • As far as I understand, you want a system that you can modify in an up-to-date way. However, the datasets you receive may contain incorrect data, as they are user-entered. I think the best solution is to get the table the way you want from the platform from which you get the data. Incorrect data can be corrected with R, but I am not sure of a general solution since it has too many parameters. I hope someone from the platform can help. – Esad Feb 22 '22 at 14:39
  • I got it what You said I will try alao with your solution. It is very weird why there is no unique ID T.T – bwjang Feb 22 '22 at 14:57