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

2021-08-09 Thread Luigi Marongiu
Got it, thank you! On Tue, 10 Aug 2021, 00:12 David Winsemius, wrote: > > On 8/9/21 12:22 PM, Luigi Marongiu wrote: > > Thank you! it worked fine! The only pitfall is that `NA` became > > ``. This is essentially the same thing anyway... > > > It's not "essentially the same thing". It IS the same

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

2021-08-09 Thread David Winsemius
On 8/9/21 12:22 PM, Luigi Marongiu wrote: Thank you! it worked fine! The only pitfall is that `NA` became ``. This is essentially the same thing anyway... It's not "essentially the same thing". It IS the same thing. The print function displays those '<>' characters flanking NA's when the cl

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

2021-08-09 Thread Luigi Marongiu
Thank you! it worked fine! The only pitfall is that `NA` became ``. This is essentially the same thing anyway... On Mon, Aug 9, 2021 at 5:18 PM Ivan Krylov wrote: > > Thanks for providing a reproducible example! > > On Mon, 9 Aug 2021 15:33:53 +0200 > Luigi Marongiu wrote: > > > df[df[['vect[2]'

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

2021-08-09 Thread Ivan Krylov
Thanks for providing a reproducible example! On Mon, 9 Aug 2021 15:33:53 +0200 Luigi Marongiu wrote: > df[df[['vect[2]']] == 2, 'vect[2]'] <- "No" Please don't quote R expressions that you want to evaluate. 'vect[2]' is just a string, like 'hello world' or 'I want to create a new column named "

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

2021-08-09 Thread Luigi Marongiu
You are right, vect will contain the names of the columns of the real dataframe buyt the actual simulation of the real case is more like this: ``` > df = data.frame(A = 1:5, B = c(1, 2, NA, 2, NA), C = c("value is blue", > "Value is red", "empty", " value is blue", " Value is green"), D = 9:13, E

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

2021-08-09 Thread Ivan Krylov
On Mon, 9 Aug 2021 13:16:02 +0200 Luigi Marongiu wrote: > df = data.frame(VAR = ..., VAL = ...) > vect = letters[1:5] What is the relation between vect and the column names of the data frame? Is it your intention to choose rows or columns using `vect`? > df[df[['vect[2]']] == 2, 'vect[2]'] '..

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

2021-08-09 Thread Luigi Marongiu
Thank you but I think I got it wrong: ``` > df = data.frame(VAR = letters[1:5], VAL = c(1, 2, NA, 2, NA)); df VAR VAL 1 a 1 2 b 2 3 c NA 4 d 2 5 e NA > vect = letters[1:5] > df[df[['vect[2]']] == 2, 'vect[2]'] <- "No"; df VAR VAL vect[2] 1 a 1 2 b 2 3 c NA

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

2021-08-09 Thread Ivan Krylov
On Mon, 9 Aug 2021 10:26:03 +0200 Luigi Marongiu wrote: > vect = names(df) > sub_df[vect[1]] > df$column[df$column == value] <- new.value Let's see, an equivalent expression without the $ syntax is `df[['column']][df[['column']] == value] <- new.value`. Slightly shorter, matrix-like syntax woul

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

2021-08-09 Thread Luigi Marongiu
Thank you very much, but that would make even more work due to the duplication... On Mon, Aug 9, 2021 at 10:53 AM Jim Lemon wrote: > > 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. > > Jim > > On Mon, Aug 9, 2021 at 6:26 PM Lu

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

2021-08-09 Thread Jim Lemon
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. Jim On Mon, Aug 9, 2021 at 6:26 PM Luigi Marongiu wrote: > > Hello, > I would like to recursively select the columns of a dataframe by > strong the names of the dataframe in a vector