Questions tagged [mutate]
426 questions
1
vote
1 answer
Why does my get_hundred function not work correctly when applied to my dataset in R using dplyr and stringr?
I've been trying to mutate a dataset with a user-defined function that includes calls to str_locate and str_sub. The aim is to locate then extract the first digit within a sequence of 3 digits amongst strings, then add this digit (as a character) to…

Klew Lesse
- 13
- 2
1
vote
1 answer
Assign value of the first row above that meets the condition
Using dplyr, how do I get for each act_id with status “Sent” the creation_date from the previous act_id whose status is “Automatic” or “Received”?
In each group by reference, each status "Sent" must find the creation date of the first "Received" or…

Antonio Y
- 13
- 3
1
vote
2 answers
How can I mutate data based on whether a string in R contains a specific value?
For merging purposes, I summarized rows of an existing dataset (structured by country, year and party) as follows, as I am not interested in the values of distinct parties, but whether a distinct kind of parties were part of the government in a…

kwalz
- 21
- 2
1
vote
1 answer
Mutating a column in R from a particular row subset
I am trying to create a new column in a dataframe based upon three other columns: a parent column, specific indicator column, and the value of the combined parent-specific indicator.
Given:
parent specific val
1 a x 10
2 a …

Reid L
- 15
- 3
1
vote
1 answer
Make a column which counts the days since a specific date
I have this data set about cows. I have calving dates and dry-off dates with some dates in between with no such events. The dates in between are irregular, meaning that there may be gaps.
It looks about like this:
ID <- c("A", "A", "A", "A", "A",…

AlHu
- 23
- 3
1
vote
1 answer
How to use purrr::map with dplyr::mutate and across in R
I've looked at a couple of previous examples with mutate, across, and map but struggled to fully understand them. Apologies if this question is a duplicate. Here are the other two posts that may be relevant - Using mutate(across(...)) with…

Tee
- 149
- 8
1
vote
1 answer
Optimize repeated fisher test over dataframe
I have a code segment that I am trying to optimize to run a bit faster.
df1 <- df %>%
rowwise() %>%
mutate(fisher = fisher.test(matrix(c(counts, nt1_not_t2,
nt2_not_t1, not_t1_or_t2), nrow = 2, ncol =…

Dave R
- 202
- 1
- 8
1
vote
1 answer
Return prior selfcalculation in mutate as an option when using a conditional
I have a one-column dataframe, and want to determine if the values are increasing (1) or decreasing (-1), and when no change is found return the last calculation done. I think the code I have should do it, but dplyr returns an error saying "object"…

Camilo
- 153
- 7
1
vote
2 answers
Create a binary column indicating the first success
Fish_ID
Instance
Success
First_Success
1
0
0
0
1
1
0
0
1
2
1
1
1
3
0
0
1
4
1
0
1
5
0
0
I have a data frame with column "Fish_ID","Instance" and"Success" and I want to create a column "First_Success", containing a 1 only at…

Balina
- 81
- 5
1
vote
1 answer
How to use stringr::str_match_all inside dplyr::mutate in the tidyverse pipe
Using stringr::str_match, I can create a column that contains the characters after "H45" for the first instance of "H45" in each row.
library(dplyr)
library(stringr)
df <- tibble::tibble(A = c("H459 A452 H4544", "A452", "H4535"))
df <- df %>%…

Susie Derkins
- 2,506
- 2
- 13
- 21
1
vote
3 answers
Issue creating new variable with mutate R using strsplit
I am trying to create new variable using mutate from column. I am using strsplit to perform some string operation to create new variable contents. But it is not working as desired.
myDF = data.frame(filename = c("T9719178_Mazda_20230415",
…

BharatAyya
- 63
- 7
1
vote
2 answers
How can I create a new column with values assigned based on conditions from rows in two columns?
I'm a beginner in R and am trying to create a new column that will have values determined by values of rows in two columns.
My data frame looks something like this:
df <- data.frame(subjectid = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3), subj_day =…

Deanna
- 13
- 3
1
vote
1 answer
Mutating a dataframe in a vectorised fashion in R programming language
Lets say I have a dataframe, called df, in R with columns like so:
id, start_period, end_period, store_value, shop_items, in_bank, owed_money, prefix_store_value, prefix_shop_items, prefix_in_bank, prefix_owed_money, val_BP_to_x, val_SR_to_x.
I also…

The Curious One
- 63
- 5
1
vote
1 answer
Add a row in data.frame by counting row numbers of another csv with names stored in the data.frame using dplyr
I have a data frame of plant Latin names, and another folder GBIF_data that stores the downloaded gbif data in csv named by the Latin names in the data frame, I want to mutate a new column to store how much data has been downloaded from GBIF for…

passiflora
- 332
- 1
- 9
1
vote
2 answers
filling up column with string and filling down sequenced based on another column
I would like to create a column named day , based on every two shift one day up to 150 days .
shif day
1 1 1
2 1 1
3 2 1
4 2 1
5 1 2
6 1 2
7 2 2
8 2 2
9 1 3
10 1 3
11 2 3
12 2 3
I was…

Malvis Va
- 13
- 4