Questions tagged [mutate]
426 questions
1
vote
1 answer
Swap start_time with end_time (vice versa) in a table using R
I am dealing with a bike sharing data set and in its raw data, the start_time has been keyed in with the end_time and vice versa. start time is later than end time
Is there a way I can swap the two columns for the affected rows or is there a way to…

Charlene Low
- 13
- 2
1
vote
1 answer
Dynamically define columns for mutate operation
For a data frame, I would like to create a new column that is the sum of other columns with dplyr's mutate(). These columns to sum over should be dynamically definable.
For example, I would like to sum two specified columns from…

MartijnVanAttekum
- 1,405
- 12
- 20
1
vote
3 answers
Performing a mutate function incorporating character strings
This is my dataframe
data.frame(
condition = as.factor(c("ecoli_RPMI", "staph_RPMI", "RPMI", "ecoli_DMEM", "staph_DMEM", "DMEM", "ecoli_RPMI", "staph_RPMI", "RPMI", "ecoli_DMEM", "staph_DMEM", "DMEM")),
time = as.numeric(c(1, 1, 1, 1, 1, 1, 2,…

William Hurt
- 37
- 6
1
vote
2 answers
Conditional calculation of new variable based on specific groups of rows, row values and columns in R dataframe
Example Data
I have got the following survey dataset in R and need help with the conditional calculation of a specific new variable.
# Load package
library(tidyverse)
# Important: set seed for replicability
set.seed(123)
# Create data: step 1
df…

Dr. Fabian Habersack
- 1,111
- 12
- 30
1
vote
1 answer
Convert set of columns using conversion key
My dataframe has a set of columns that starts with "i10_dx". I would like to use the conversion key I have to create a new set of columns starting with "ccs" that corresponds to the former set of columns.
This is a snippet of dataframe
df <-…

ltong
- 501
- 1
- 8
1
vote
2 answers
Creating new column using existing column names and values
I am trying to create a new column using existing column names within my dataset. Specifically, in this dataset, one row corresponds to one respiratory specimen and has ~10 virus test results, each with a corresponding column; a value of 0…

jcijac89
- 11
- 1
1
vote
1 answer
na.rm = TRUE is not working with mutate or summarise
I have a dataset with a lot of NA but on the same row's are data which are important so I cannot remove the rows. I also cannot replace the NA with a 0 because i want to get an average.
What can i do?
example of R script:
data_week1 <- data_calc…

Kristel
- 21
- 1
1
vote
1 answer
R: Help using group_by( ) then mutate( ) to add a column into the df counting data up
I have a data frame where the data is chunked into blocks of 10 trials. The data frame does not have a "trial_within_block" variable and I want to add one. I have used the following code:
data <- data %>%
group_by(subject, block) %>%
…

Matthew Babb
- 15
- 3
1
vote
1 answer
duplicated() in combination with mutate() and lapply() on lists of characters: duplicate identification
I want to identify duplicated characters in grouped lists
Consider the following example data frame:
ID<-c("Carl",…

Bettina
- 35
- 6
1
vote
0 answers
Create a lag up to a specific value in a column R
I have a timeseries dataset in R with 17 time points. For each group of individuals (named ID), individuals were infected at the beginning of the time series (at timepoint 0). In some groups, individuals were re-infected one or more times throughout…

bellbyrne
- 67
- 7
1
vote
1 answer
Looping over data frame to "clean" data
This is the kind of data I have:
Date
Station
Param1
Param2
2020-01-01
A
<5
45
2020-02-01
B
<5
47
To be able to plot this data, mark the LOQ-values (<5) and compute some basic statistics, I need to create new columns with the LOQ-flag…

Martin Liungman
- 37
- 5
1
vote
1 answer
Calculate new variable based on condition RStudio
I want to create a new variable. Depending on the expression of variable X, variable Y is to be divided by one of three values.
I tried tho following code, but it doesn't work and I don't know how to do it in another way...
df %>% mutate(new_var =
…

Nori
- 13
- 3
1
vote
1 answer
How do I pass an entry (group of entries) to the function that is used within mutate in R?
I am writing the code that will identify what category does an entry in a table (target table) belongs to, comparing that entry to another table (code table).
For example, the code table is:
df_code <- data.frame("Colour" = c("Blue", "Red",…

Makie
- 37
- 4
1
vote
2 answers
How can I create a new variable that marks the lowest four test scores in each classroom as a 1?
I have a dataset with 500 observations. Two of the variables are "test" which is a continuous variable of a test score and "classroom" which is a numerical variable indicating which classroom (1-20) the observation is nested in.
I want to create a…

A Hayward
- 11
- 1
1
vote
1 answer
Best way to replace a variety of write-in answers with numbers using R
I am using R to clean a dataset. Part of my dataset looks like:
record_id | organization | other_work_loc
1 12 CCC
2 12 AMG
3 12 TAO
4 1
5 2
6 …

Phoebe Long
- 11
- 1