0

I have two data frames. One is called dfnew15 and the other one is called dfnew22. I did some easy data cleaning with the dfnew15 data frame with about 100 lines of code. Each line of code features the name "dfnew15$column1" "dfnew15$colum2".

Is there a way to change all of those dfnew15 to dfnew22 without clicking in and changing them manually? If so, how would that work? I want to make sure I would not miss anything from 15 in the 22 data frame, but doing what I want manually is a pain.

The line of code I would like to change on mass is a variation of:

dfnew15$Town <- ifelse(dfnew15$Town == "Manhattan", "New York City", dfnew15$Town)
dfnew15$Town <- ifelse(dfnew15$Town == "Bay Area", "San Francisco", dfnew15$Town)
etc.

I thought I might just be able to copy the R-script text, paste it into a txt file, import that into my global environment and then do

txtfileAsDataFrame <- gsub('dfnew15', 'dfnew22', txtfileAsDataFrame)

export the txtfileAsDataFrame and then just copy the changed document into a fresh script with all my data frames being called dfnew22.

But unfortunately I only get the changed data as a value, not as a data frame. Also I would guess there to be a more elegant option to do this than importing and exporting txtfiles.

Thank you for any help, hint or tip!

Mark
  • 7,785
  • 2
  • 14
  • 34
  • 2
    The more general solution to this is to write a function to do the transformation, and then you pass in whatever dataset you want to that function and the transformation happens there. You want to avoid ever hard coding a specific data.frame over and over again. If you have a ton of if/else statements like that, it looks like it would be a lot more efficient to merge data in anyway. – MrFlick Feb 02 '23 at 21:45
  • 2
    As an added discussion, it sounds like your two frames have the same structure. In that case, it is generally good practice (and much easier in the long run!) to work with a [list of frames](https://stackoverflow.com/a/24376207/3358227). Once you have such a list of frames (e.g., named `LoF`), use a function that you wrote (based on MrFlick's excellent suggestion), you can do `lapply(LoF, myfun)` and do that collection of data-processing on each of the frames. – r2evans Feb 02 '23 at 22:10
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 02 '23 at 22:27
  • Well thank you guys very much for the answers provided. I talked to my friend just right now and he said "why don't you just ctrl+F and replace in a new script?". I wasn't aware of that function and now I feel kinda dumb. That was actually the easiest option for me, but I will look into your suggestions to learn them for future projects! Thanks! – Pietro Pisellone Feb 03 '23 at 09:41

1 Answers1

0

Well thank you guys very much for the answers provided. I talked to my friend just right now and he said "why don't you just ctrl+F and replace in a new script?". I wasn't aware of that function and now I feel kinda dumb. That was actually the easiest option for me, but I will look into your suggestions to learn them for future projects! Thanks!

- OP

Other options include:

  1. creating a function to do the transformations, then running the function on the required dataframe (credit: Mr. Flick)
  2. using pipes (e.g. ) and to chain together a bunch of data transformations (so then, if you need to change which dataframe it applies to, you can just change the code from df1 |> foo() etc. to df2 |> foo() etc.
  3. in the case where you have a bunch of code which is transforming df1, but you want it to transform df2, a hacky way could be to rename df1 to df3, then rename df2 to df1, transform this new df1, change the name back to df2, then change df3's name back to df1

The first option is the best if you're going to be transforming the data regularly, or are going to be applying it to many dataframes (credit: r2evans). The second option is best if you only plan to do the transformation to one dataframe. The third option is probably the worst option in all scenarios

Hope this helps!

Mark
  • 7,785
  • 2
  • 14
  • 34