[R] How to modify rows matching patter on multiple columns of dataframe?
Kimmo Elo
k|mmo@e|o @end|ng |rom utu@||
Thu Aug 12 15:17:25 CEST 2021
Hi,
something like this (a customised version based on this:
https://stackoverflow.com/questions/25768305/r-replace-multiple-values-in-multiple-columns-of-dataframes-with-na
):
--- snip ---
col_idx<-grep("^var[123]", names(df))
m1<-as.matrix(df[,col_idx])
m1[m1=="a"]<-"z"
df[col_idx]<-m1
df
--- snip ---
HTH,
Kimmo
to, 2021-08-12 kello 14:37 +0200, Luigi Marongiu kirjoitti:
> Helo
> I have a dataframe whose names are similar and I would like to change
> the rows containing given values simultaneously.
> I can select the columns using library(dplyr) but I can't modify the
> data:
> ```
> library(dplyr)
> > df <- data.frame(var1 = c(letters[1:3], letters[1:4]),
> + var2 = c(LETTERS[1:7]),
> + var3 = c(letters[1:3], letters[1:4]),
> + var4 = (1:7)^2,
> + var5 = c("light", "light", "heavy", "heavy",
> "heavy",
> + "light", "heavy"),
> + stringsAsFactors = FALSE); df
> var1 var2 var3 var4 var5
> 1 a A a 1 light
> 2 b B b 4 light
> 3 c C c 9 heavy
> 4 a D a 16 heavy
> 5 b E b 25 heavy
> 6 c F c 36 light
> 7 d G d 49 heavy
> > select(df, matches("var[123]"))
> var1 var2 var3
> 1 a A a
> 2 b B b
> 3 c C c
> 4 a D a
> 5 b E b
> 6 c F c
> 7 d G d
> > df[[select(df, matches("var[123]")) == "a"]] <- "z"
> Error in `[[<-`(`*tmp*`, i, value = value) :
> recursive indexing failed at level 2
> > df[[select(df, contains("var1")) == "a"]] <- "z"
> Error in `[[<-`(`*tmp*`, i, value = value) :
> recursive indexing failed at level 2
> ```
> If I sue which, I get a wrong substitution :
> ```
> > df[which(select(df, matches("var[123]")) == "a"), ] <- "z"; df
> var1 var2 var3 var4 var5
> 1 z z z z z
> 2 b B b 4 light
> 3 c C c 9 heavy
> 4 z z z z z
> 5 b E b 25 heavy
> 6 c F c 36 light
> 7 d G d 49 heavy
> 8 <NA> <NA> <NA> <NA> <NA>
> 9 <NA> <NA> <NA> <NA> <NA>
> 10 <NA> <NA> <NA> <NA> <NA>
> 11 <NA> <NA> <NA> <NA> <NA>
> 12 <NA> <NA> <NA> <NA> <NA>
> 13 <NA> <NA> <NA> <NA> <NA>
> 14 <NA> <NA> <NA> <NA> <NA>
> 15 z z z z z
> 16 <NA> <NA> <NA> <NA> <NA>
> 17 <NA> <NA> <NA> <NA> <NA>
> 18 z z z z z
> ```
>
> what is the correct syntax?
> Thank you
>
More information about the R-help
mailing list