Questions tagged [mutate]
426 questions
2
votes
3 answers
rowsums of specific columns filling a condition
I'm trying to sum specific columns of my data that fills some condition,
like the example underneath
library(dplyr)
library(readr)
set.seed(123)
data=data.frame(id=1:4,
v1=sample(c("a","b"),4,TRUE),
…

Wael
- 1,640
- 1
- 9
- 20
2
votes
2 answers
Mutate and if else function to create new column
It may be a very easy question, but so far I failed.
My dataset looks like this
Duration
Unit
1
day
3
month
5
weeks
What I want to do is to create a new column for the number of days depending on the unit. So 3 months should be 90…

USER12345
- 45
- 5
2
votes
2 answers
Mutate value of a range of columns if columns name meets another column value
I have a wide df with columns representing the months of many given years and the changes of colour in each month:
df <- data.frame(id = as.integer(c(123,124,125,126)),
no_change = as.character(c("May.2010", NA, NA, "Sep.2010")),
…

Nao
- 333
- 2
- 11
2
votes
2 answers
Find the very last character of the previous row in R
Let's say I have a dataframe like this:
d <- data.frame(order = c("101", "01", "10", "01", "101"),
dur_1 = c(50, 20, 40, 25, 45),
dur_2 = c(40, 30, 45, 34, 96),
dur_3 = c(20, 0, 0, 0, 125))
What I…

Lee
- 369
- 1
- 6
2
votes
2 answers
How to make four combination of points with the same ID in a data frame in R?
I am currently trying to make squares (polygons) from a data frame in R and in order to do that (according this guide), I need to have a data frame with 4 sets of paired points as their lon-lat coordinates.
Using this example:
sample_df <-…

cinnamoroll
- 23
- 4
2
votes
0 answers
How to subtract values from a specific column depending on whether it has been tagged with one or more IDs, in R?
Note: this is somewhat related to a question I have previously asked here
Here is a subset of what my data looks like, as an example:
library(dplyr)
DF <- structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19),…

kiwi
- 565
- 3
- 11
2
votes
3 answers
Unable to specify column names with space
Although this post showed that one could use backtick ` to select columns that had space in their names, I couldn't do that in the following code
library(tidyverse)
library(survival)
df <- colon[colon$etype==2, c("time", "age")]
num_summary <-…

Nemo
- 1,124
- 2
- 16
- 39
2
votes
2 answers
Mutate in a group by taking mean of only the rows in the group that equal the selected row on another column
I'm running into an issue where I am grouping a data set by one variable (group) and want to take create a new output column that is the mean of the value column within each group AND with the added condition that we only want to consider values…

Stew Guffin
- 23
- 4
2
votes
1 answer
Why does one method of using mutate and sum with an ifelse produce a different outcome than just using mutate and sum in R?
I have written in R three different methods to make the variable S1_Total_Time that is the sum of a number of columns using dplyr mutate. When I run them, I get different outputs for the variable S1_Total_Time. In my dataframe each line is a person,…

Anne McLaughlin
- 21
- 2
2
votes
1 answer
How to index a purrr map call within a for loop
I am working with list columns. One column is the the data of interest, and another is a list of row indices for the the data. I would like to dynamically add and title new columns to the List column. Each new column should be a subset of the…

abeyer42
- 23
- 2
2
votes
3 answers
Using mutate to add up multiple pairs of variables ending with the same number
I have a dataframe with variables V1, V2, e1, e2 and I want to add up V1 and e1, and V2 and e2. It should work for numbers 1 to n, of which n is an argument of a function in which this code is embedded in.
The following code is what I have now, and…

Julian Sagebiel
- 31
- 3
2
votes
2 answers
error in `na_if()`: ! Can't convert `y` to match type of `x`
I have a dataframe df_3 from which I want to mutate multiple columns starting with Team_. I want to replace 0s contained in the columns with NA. I use a code which I have previously successfully used but now gives me the following error:
Error in…

Soph2010
- 563
- 3
- 13
2
votes
1 answer
dplyr mutate compare with another data frame
I have 2 data frames like this:
A:
col1 col2
1 a
1 b
1 b
1 c
1 c
2 x
2 y
2 y
3 k
3 k
3 m
3 m
B:
col1 col2 col3
1 a 0.3
1 b 0.001
1 c 0.0004
2 x 0.005
2 y 0.09
3 k 0.00007
3 m 0.08
What I want to do is to create another col3 on A using mutate and…

cookiemonster
- 109
- 7
2
votes
3 answers
Extract first Non NA value over multiple columns
I'm still learning R and was wondering if I there was an elegant way of manipulating the below df to achieve df2.
I'm not sure if it's a loop that is supposed to be used for this, but basically I want to extract the first Non NA "X_No" Value if the…

bilmawr
- 41
- 5
2
votes
2 answers
Specify levels when `mutate`-ing dataframe variables to factors
Let's say I have the following tibble dataframe called data:
library(tibble)
data <- tribble(
~"ID", ~"some factor", ~"some other factor",
1L, "low", "high",
2L, "very high", "low",
3L, "very low", "low",
4L, "high", "very…

hpy
- 1,989
- 7
- 26
- 56