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

2021-08-12 Thread Rui Barradas
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 #1zAz1 light #2bBb4 light #3cCc9 heavy #4

[R] ggplot: add percentage for each element in legend and remove tick mark

2021-08-12 Thread Kai Yang via R-help
Hello List, I use the following code to generate a donut plot. # Compute percentages eth$fraction = eth$individuals / sum(eth$individuals) # Compute the cumulative percentages (top of each rectangle) eth$ymax = cumsum(eth$fraction) # Compute the bottom of each rectangle eth$ymin = c(0, head(eth$yma

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

2021-08-12 Thread Kimmo Elo
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 --

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

2021-08-12 Thread Eric Berger
Hi Luigi, I would take a slightly different approach. Maybe this is helpful. idV <- grep("var[123]",colnames(df)) df[,idV][df[,idV]=="a"] <- "z" df var1 var2 var3 var4 var5 1zAz1 light 2bBb4 light 3cCc9 heavy 4zDz 16 heavy 5b

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

2021-08-12 Thread Luigi Marongiu
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 =