[R] substitute column data frame based on name stored in variable in r

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Mon Aug 9 10:53:29 CEST 2021

Hi Luigi,
It looks to me as though you will have to copy the data frame or store
the output in a new data frame.


On Mon, Aug 9, 2021 at 6:26 PM Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
> Hello,
> I would like to recursively select the columns of a dataframe by
> strong the names of the dataframe in a vector and extracting one
> element of the vector at a time. This I can do with, for instance:
> ```
> vect = names(df)
> sub_df[vect[1]]
> ```
> The problem is that I would like also to change the values of the
> selected column using some logic as in `df$column[df$column == value]
> <- new.value`, but I am confused on the syntax for the vectorized
> version. Specifically, this does not work:
> ```
> sub_df[vect[1] == 0] = "No"
> ```
> What would be the correct approach?
> Thank you
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list