Questions tagged [mutate]
426 questions
1
vote
1 answer
Using case_when with dplyr mutate: why is sometimes the value per row used for calculation and sometimes the whole column?
I want to create a new variable when two variables match a condition. In that case I want to have the smaller of two other variables. In my real data these are dates and I want to add other conditions in the case_when function, but I keep it simple…

PStaus
- 15
- 4
0
votes
3 answers
Conditionally replace cell dependent on match in other column
Consider the following simplified data frame:
df<-data.frame(x1=c("A","B","C"),x2=c("K to B","K to B","K to B"))
I want to replace strings in x2 with NA (or "") in the rows where the x1 character cannot be found as part of x2. That is, the data…

Bettina
- 35
- 6
0
votes
2 answers
How to perform a loop with sym() in r?
I have a data frame with several continuous variables. I need to generate the logarithm of each continuous variable. The names of each new variables must be made up of the original variable name adding _ln. I would like to generate a loop to do this…

DavidMB
- 7
- 3
0
votes
1 answer
Differential variable assignment using mutate
I apologize in advance for asking this painstaking question.
I have a dataset that looks like this (there are about 1000 rows total, this is just the head). Each column is a z-scored blood analyte.
structure(list(alt_zscore = c(1.15628571428571,…

burphound
- 161
- 7
0
votes
0 answers
Using a function dictionary and mutate to assign groupings
Background for context to my question:
I have data on metabolic, immune, and neuroendocrine analytes. I am creating a cumulative deficit index, which is a cumulative measure of analytes that are in dysregulation.
To create the index: All analytes…

burphound
- 161
- 7
0
votes
2 answers
Separate data from a single column data frame
I have a data frame that looks like this
Reproduceable code can be found here: https://gist.github.com/jeffgswanson/703bb9eb1698518d1dd9aec43e91fefd
I want to break this up into four columns: Away Score, Away Team, Home Score, Home Team.
The…

Jeff Swanson
- 45
- 1
- 8
0
votes
1 answer
How can I create new rows based on data from a single row?
I have data (df) containing data where each row is a different school. Each school has its own ID number, the number of students who failed maths gcse, the number who passed, and the number who sat the gcse.
E.g. for school
School…

Jess
- 11
- 2
0
votes
1 answer
R - Why can't I match country codes to custom dictionary?
I'm working with gtap data and would like to combine it with other datasets. I am trying to find a way to use country ID's/codes, and it seems one option is to use the R package countrycodes. However, gtap is not included in the supported codelist…

MoonS
- 117
- 7
0
votes
0 answers
How to convert POSIXct to date?
My excel data is written in POSIXct format (see below)
final_sample <- data.frame(
PN_number = c( "PN101", "PN102", "PN103", "PN104", "PN105"),
Maturity_date_2018 = c(1518048000, 1516665600, 1518134400, 1518048000, 1520812800),
…

Yhan
- 3
- 1
0
votes
2 answers
How do I create a new column for subsetting an ID column in an R dataframe?
I have a dataframe with an ID column that has multiple observations per unique ID. I also have a column that has a binary value - "commute" or "cluster". How it usually goes is about a dozen or so observations of cluster broken up by one or two…

Matt-W22
- 1
- 2
0
votes
0 answers
Date range in r
I have this dataframe, with the following variables:
amostra_coc02 <- c("AI-0006-LT01-T1-1", "AI-0006-LT01-T1-1")
equipamento_coc02 <- c("FZ-001-CP", "FZ-001-CP")
data_coc02 <- c("2022-06-09", "2022-06-09")
data_coc03 <- c("2022-06-13",…

fdpr
- 1
- 1
0
votes
1 answer
Mutate using multiple conditions based on dataframe columns
I have this tibble (transactions)
A tibble: 13 × 2
START_DATE CHANNEL_LVL1
1 NA Website
2 2023-01-31 Website
3 NA WalkIn
4 2023-01-31 Marketplace
5 NA Marketplace
6…
0
votes
2 answers
Mutate in place and if_any with condition over multiple columns
I am using R dplyr and trying to mutate multiple columns in place. These columns are var1-var3, and they currently contain values of either 1 or NA.
I want to apply the following logic: if a given row in var1-var3 has at least one value = 1, then…

saddas
- 65
- 7
0
votes
1 answer
Converting and Rounding Numeric Variables with NAs
I am working with a dataset called fab_2023, which I imported from Excel. Among the variables in the dataset, I need to convert certain ones into numeric format. However, these variables do contain missing values (NAs). Additionally, the original…

e. erhan
- 61
- 6
0
votes
3 answers
Fomart date with date_parser
I'm using R to extract data from multiple pdf files. After extracting this data, I need to format a field of type date. Dates came from pdf files in various formats
data = c( "1/10/2022 2:36:00 pm",
"1/11/2022 12:47:00 pm",
"1/12/2022 9:47:00…