Questions tagged [mutate]
426 questions
5
votes
3 answers
How to split values separated by commas in the same row to different rows in R
I have some data from a a Google Forms and I'd like to slipt the common-separated answers and duplicate the participant's ID
The data looks like this:
> head(data)
names Q2 Q3 Q4
1 PART_1 …

Larissa Cury
- 806
- 2
- 11
3
votes
3 answers
Can we avoid repeating the column name each time when using it in mutates
I use several mutate on the same column. How can we only use mutate once and without repeating the column name?
df <- data.frame(
c1 = c("Élève", "Café", "Château", "Noël", "Crème")
)
df2 <- df %>%
mutate(c1 = trimws(c1)) %>%
mutate(c1 =…

dia05
- 57
- 4
3
votes
1 answer
How to create new columns more conveniently with partially matched names?
Below code create new variable a_new/b_new/c_new , but have to input code in mutate one by one. Is there any convenient way ?
In actual, I have to create many variable ,for instance a_new/b_new/..../z_new. I don't want to input the variable…

anderwyang
- 1,801
- 4
- 18
3
votes
2 answers
Faster way to add a calculated column
I have a dataframe in which I want to check some condition and need to add a new column based on result of condition.
This is my input data
InputData = data.frame(A = c("", "", "Apple"), B = c("", "", "Orange"), C = c("", "", ""), D = c(0, 1,…

ParthaSarathi
- 187
- 7
3
votes
3 answers
Replacing unique values in vector with names based on a key
I have vector containing several unique values:
codes <- c(122, 108, 122, 202, 122, 113, 122, 108)
Each of these values corresponds to the name for a specific tree species:
122 - ponderosa,
108 - lodgepole,
113 - limber,
202 - Douglas fir,
I have…

JL435
- 35
- 4
3
votes
3 answers
How do I mutate across to multiple columns together that have similar names in R?
I have many columns that have same names that always start with the same string, either n_ for the number of students, score_ for the percent of students who passed, and loc_ for the room number.
In this, I want to multiple the n_ columns with their…

J.Sabree
- 2,280
- 19
- 48
3
votes
3 answers
Concisely assign vector output of a function to multiple variables in dplyr
I am trying to assign the vector output (i.e. greater than length 1) of a function to multiple columns in a single operation (or at least as concisely as possible).
Take the range() function for example which returns as output a numeric vector of…

henhesu
- 756
- 4
- 9
3
votes
5 answers
dplyr mutate and purrr map: use data masking to select columns for map
In a dplyr mutate context, I would like to select the column a function is applied to by purrr:map using the value of another column.
Let's take a test data frame
test <- data.frame(a = c(1,2), b = c(3,4), selector = c("a","b"))
I want to apply…

taxecron
- 33
- 4
3
votes
2 answers
How do I create many new variables in R, each of them based on multiple other variables?
I have a large R data frame (>1 million records with >1,000 variables) that captures information about patient visits to hospitals. Included in this data frame are 30 different procedure statuses, locations, and codes: ProcedureStatus01,…

Norwooder
- 35
- 4
3
votes
2 answers
replace for loop with dplyr across / rowwise?
I'm having a hard time refactoring a for loop into a dplyr pipe.
I need to reference the dataframa a and the previously calculated row.
Any advice how to get b from a on a dplyr pipe?
Many thanks!
a <- tibble::tribble(~ 'a', ~ 'b', ~ 'c',
…

cg1979
- 100
- 5
3
votes
3 answers
Add lists as variables in a dataframe
I need an efficient way to add a series of variables together, preferably without a loop.
library(dplyr)
avar_2000 <- c(2,5,1)
avar_2001 <- c(2,3,1)
avar_2002 <- c(7,2,5)
bvar_2000 <- c(9,1,1)
bvar_2001 <- c(5,5,3)
bvar_2002 <- c(3,8,NA)
df…

Andre
- 101
- 4
3
votes
2 answers
In dplyr mutate across, is it possible to use non-referenced columns in a programmable fashion?
Suppose I have this tibble with an arbitrary number of variable pairs x and x_var, y and y_var, etc.
dt <- tibble(x = 1:3,
y = 2:4,
z = 3:5,
x_var = rep(0.1, 3),
y_var = rep(0.2, 3),
z_var = rep(0.3, 3))
I was…

lmyt
- 71
- 2
2
votes
2 answers
mutate (calculate) variables from one column based on specific row conditions in another col
Here is the example dataframe:
data.frame(sample = c('A','A','A','A','A','B','B','B','B','B'),
measure = c(20,30,40,60,60,20,60,50,40,10),
time = c(1,2,3,4,5,3,4,5,6,7),
start =…

yixu501
- 23
- 3
2
votes
4 answers
Creating multiple NEW columns using across() in R
The difference between my question and existing questions is that I want to create new columns with mutate that do not depend on existing columns.
Some dummy data:
library(dplyr)
dat <- tibble(
a = 1:5,
b = LETTERS[1:5]
)
I know I can…

Earlien
- 145
- 9
2
votes
2 answers
How can I use mutate to create new variables in nested for loops?
I have a dataset with unique Participant_IDs that are each rated by two different Rater_IDs on many different variables (Q1, Q2, and Q3 here). I am trying to find a way to compute a variable which indicates whether the two raters' ratings are within…

Alexa
- 23
- 4