[R] How to modify rows matching patter on multiple columns of dataframe?

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Aug 12 18:59:10 CEST 2021


Hello,

And another way, with which(., arr.ind = TRUE).
In two steps, to make it clearer.


i <- which(df[grep("var[123]", names(df))] == "a", arr.ind = TRUE)
df[i] <- "z"

df
#  var1 var2 var3 var4  var5
#1    z    A    z    1 light
#2    b    B    b    4 light
#3    c    C    c    9 heavy
#4    z    D    z   16 heavy
#5    b    E    b   25 heavy
#6    c    F    c   36 light
#7    d    G    d   49 heavy


Hope this helps,

Rui Barradas


Às 13:37 de 12/08/21, Luigi Marongiu escreveu:
> 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