1

I have a list with names of 30 columns in a dataframe.

I have to apply replace to these 30 columns only. I tried:

list = names(df[!,r"Com"]) - ok!

Then I am trying:

replace!(df[!,list],"RR" =>"AA") - Fail

How can I do such transformation(with certain columns at once)?

merchmallow
  • 774
  • 3
  • 15

1 Answers1

1

There are several ways to do it. In what I write below I assume you want to update the df data frame in-place (i.e. you do not want a new data frame, but change the existing one).

foreach(col -> replace!(col, "RR" => "AA"), eachcol(df[!, r"Com"]))

or

mapcols!(col -> replace!(col, "RR" => "AA"), df[!, r"Com"])

or

transform!(df, Cols(r"Com") .=> col -> replace!(col, "RR" => "AA"), renamecols=false)
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107