Questions tagged [mutate]
426 questions
1
vote
2 answers
Row bind multiple columns into two columns averaging unique elements in order
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 take a distinct on each V(X)_ID and average…

bilmawr
- 41
- 5
1
vote
2 answers
In R, how do I make a long dataset that contains all the categories that a row applies to?
I have a dataset of students, and I want to create a column that categorizes each student based on their and other students' data in their "subject" and "grade_letter" column because I eventually want to create a column that says "math_science_A",…

J.Sabree
- 2,280
- 19
- 48
1
vote
0 answers
how can we solve Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "c('double', 'numeric')"?
I have a file in csv format which contains two columns: samples and status(pLGG and healthy)
Now I intend to add a column which turn 1 for pLGG samples and 0 for healthy ones.
In fact, I want to binarize my categorical variable for further analysis…

Sara Tavallaei
- 11
- 3
1
vote
2 answers
In R, how can I get the sum for all combinations of two variables?
I have a long dataset with students grades and subjects. I want to keep a long dataset, but I want to add a column that tells me how many Fs a student had in their humanity courses (English & History) and their STEM courses (biology & math). I also…

J.Sabree
- 2,280
- 19
- 48
1
vote
2 answers
Difference from the mean by column
I've got this DF:
structure(list(Date = structure(c(18605, 18604, 18598, 18597,
18590, 18584, 18583, 18578, 18570, 18569, 18563, 18562, 18557,
18549, 18548, 18542, 18541, 18536, 18534, 18529, 18521, 18520,
18515, 18508, 18500, 18499, 18493,…

alec22
- 735
- 2
- 12
1
vote
0 answers
Is there a way to not show rows below a certain value in a table using Kable/KableExtra
I am trying to get a table for my dataset that only shows rows which are above a certain value, but which still uses the numbers in those rows to get the means for the supersets. using the df diamonds, i have the following code. What I want is for…

sscoresby
- 67
- 5
1
vote
1 answer
Using Janitor based on other columns in the same row to output conditional results
What I'm trying to do is have if the row from ColX is in the row ColZ I want a new column to be ColZ if not its colA.
I kept looking around but couldn't find a solution.
My data is a bit more in depth but this example should do it.
Perhaps there is…

Fugles
- 151
- 7
1
vote
1 answer
How to use custom function() and if_else with grep to recode values in R
I am trying to create a custom function that I can apply across various columns to recode values from characters to numeric data. The data has many blanks and each character entry is the same in each given column (ie. when there is a survey question…

Allison
- 33
- 2
1
vote
2 answers
How to preserve column names after applying scale()?
With the database pasted as example you can see how the following pipe to scale() everything but the group of intervention (categorical variable), the column names change the name to: variable_name[,1], when I just want variable_name. It seems that…

Javier Hernando
- 181
- 7
1
vote
1 answer
Mutating a column of grouped data with a value from a column based on a specific timestamp from another column
I am looking for an efficient way to replicate a process below, for example using dplyr.
Suppose I have this df:
df <- data.frame(id = c("A", "A", "A", "A", "A", "A", "A", "A"),
date = as.Date(c("2022-01-01", "2022-01-01",…

nlp
- 151
- 5
1
vote
2 answers
Rename/recode variable value in R based on condition using dplyr
I have a dataset dataExtended with variable CountryOther and n which is a count of wines in that particular country. CountryOther is character type and n is integer. What I want to do, is to rename values in CountryOther to Other in case the n <=20.…

Alzbeta Ivanovicova
- 35
- 5
1
vote
1 answer
R New column with repeating value from another column based on min or max of reference column
The answer for this question "R Create a column containing value of another column based on min or max of reference column" does what I want but it's not working when the reference column has more than a ones digit or is a date.
library(dplyr)
df <-…

Robin
- 465
- 5
- 11
1
vote
0 answers
Merge adjacent polygons in sf
I'm trying to merge groups of adjacent polygons, but I'm getting big multipolygons with non-adjacent areas. In the code block below plot(Matsuyama.sf) shows a large contiguous region and a few islands, but I can't extract those geometries. How do I…

Mark R
- 775
- 1
- 8
- 23
1
vote
1 answer
New identifier column to dataframe based on whether string contains said identifier
I am an absolute novice to R. What I would like to achieve is to have an identifier added to each dataframe row based on whether a string value in the same row contains that identifier.
Assume dataframe:
df <- data.frame(Code = c("DE8230", "18FR16",…

Brendan031
- 13
- 3
1
vote
1 answer
Change character values under specific columns that ends with similar suffixes in R
Hello friends I have a data frame like this:
# Define the column names
col_names <- c("A_1C_1", "B_1C_1", "C_1C_1", "D_1C_1", "E_1C_1", "F_1C_1", "G_1C_1", "H_1E_1", "I_1D_1", "J_1D_1")
# Define the data frame
df <- data.frame(replicate(10,…

dplyr
- 83
- 5