Skip to content

R - How to replace values in a column AND save to new dataframe? #1353

Answered by kubu4
kubu4 asked this question in Q&A
Discussion options

You must be logged in to vote

Well, I've opted for this approach:

  1. Duplicate original data frame by assigning to new data frame with different name.
  2. Run that substitution code on the new data frame which performs the replacement "in place".
mutated_sample_metadata_subset <- sample_metadata_subset
mutated_sample_metadata_subset$TreatmentN[mutated_sample_metadata_subset$TreatmentN == 4] <- 0

For me, it's a bit cleaner and easier to read. Thanks for all the help @laurahspencer !

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
2 replies
@kubu4
Comment options

kubu4 Dec 2, 2021
Maintainer Author

@kubu4
Comment options

kubu4 Dec 2, 2021
Maintainer Author

Comment options

You must be logged in to vote
3 replies
@kubu4
Comment options

kubu4 Dec 2, 2021
Maintainer Author

@laurahspencer
Comment options

@kubu4
Comment options

kubu4 Dec 2, 2021
Maintainer Author

Comment options

kubu4
Dec 2, 2021
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by kubu4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants