1

There are 2 data frame. df1 is the original data and df2 is the output data (changed data) - but the original columns are displayed as they are.

I'd like to change column name and value (df2 → df1).

Please help me.

  1. Change "ch_id, ch_age, ch_score" name to id, age, score
  2. Change value df2 -> df1 (age, score)

Example:

id <- c("1", "2", "3", "4", "5", "6", "7")
age <- c(24, 28, 31, 25, 27, 22, 29)
sex <- c("male", "female", "female", "male", "female", "male", "female")
score <- c(90, 80, 85, 75, 95, 80, 70)

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

ch_id <- c("1", "2")
ch_age <- c(50, 40)
ch_score <- c(80, 80)

df2 <- data.frame(ch_id, ch_age, ch_score, stringsAsFactors = FALSE)
df2
benson23
  • 16,369
  • 9
  • 19
  • 38
bwjang
  • 5
  • 6

1 Answers1

0
df2 <- data.frame(id=ch_id, age=ch_age, score=ch_score, stringsAsFactors = FALSE)

To change df2's id,age,score into df1's id,age,score:

df2 <- df1[,-3]
algae
  • 407
  • 4
  • 15
  • thanks for your answer. but I`d like to change the value after the making data.frame (T.T) is there good solution ?? I want the df2 value including original value (df1) / overwrite changed value – bwjang Feb 18 '22 at 06:35
  • original column: id age sex score original row: 7 lines your logic is displayed only id age score (changed value), I`d like to display original columns & rows – bwjang Feb 18 '22 at 06:40
  • original columns and rows? You mean `df1`? If you want to change the entries of the frame, e.g. for `id` you can simply `df1$id <- c(50, 40)`. Could you clarify what you'd like the final output to be? – algae Feb 18 '22 at 07:06
  • ``` original columns: df1 (its correct) expected output - column: id age sex score - row: 7 lines data with changed data (according to example it is age, score) ``` – bwjang Feb 18 '22 at 07:16
  • - column: id age sex score – bwjang Feb 18 '22 at 07:21
  • - row: 7 lines data with changed data (according to example it is age, score) – bwjang Feb 18 '22 at 07:21
  • What 'changed data' and what example? Your `df2` is missing a sex column, so you can't combine simply with `rbind(df1, df2)`. – algae Feb 18 '22 at 07:53
  • Yes I know. df2 is changed value, exactly I want to update the original data with changed value (df2 = age & score) Sorry to difficult explanation – bwjang Feb 18 '22 at 08:00
  • Okay, but you don't know the sex for df2. So do something like this, `df2 <- data.frame(id=ch_id, age=ch_age, sex=rep(NA,length(ch_id), score=ch_score), stringsAsFactors = FALSE)` and then `df3 <- rbind(df1, df2)` – algae Feb 18 '22 at 08:04
  • wow I think it is helpful. I will run it !!! – bwjang Feb 19 '22 at 01:36
  • owe T.T algae. thanks you helped me but i wasn`t work maybe I didn`t give you exactly what i want. so i added question 1more time please refer below url. \ https://stackoverflow.com/q/71221647/18062678 – bwjang Feb 22 '22 at 13:14
  • You need to give a reproducible example. _Write down what you want as an output and include it in your post_. – algae Feb 22 '22 at 21:47