Questions tagged [mutate]

mutate is a verb to create a new column in a data frame using dplyr package in R programming language.

When using dplyr for data wrangling, mutate is commonly used to add a new column to a data frame. With a data frame df, mutate(df, a = 3) adds a constant column 3 to the data frame. For more information on related verbs in dplyr and more examples on how to use mutate check the official help page.

Note that as of dplyr version 1.0.0, the scoped helpers (including mutate_if, mutate_at and mutate_all) have been superseded by across() (see the official change-log).

1668 questions
0
votes
1 answer

how to avoid For loop to calculate new varaible by group

I hope someone here could help me. I am trying to learn R coding for my work. I am following the growth of plants ( called Ecotype) over the time, they are Treated with Mock or a bacteria Xcc. I have 2 diffrent experiements (done at different…
mehdi
  • 21
  • 2
0
votes
1 answer

Selecting certain values in dataset and changing their values

I have the tbl format dataset for my project and I have one column where I have mixed values (although I can differentiate which are which) and I want to change the selected values of that column by dividing them by certain number, what is my best…
0
votes
1 answer

Boxcox transformation on multiple variables with mutate_at

Say, I want to do boxcox transformation from caret package on the following data (not the data I am working with but just to explain my problem): library(caret); library(tidyverse) set.seed(001) d <- tibble(a = rpois(20, 10), b = rnorm(20, 40, 10)) …
esm
  • 95
  • 7
0
votes
1 answer

Problem while using Group_by/mutate function in R

I am trying to calculate the variance between preceding rows using group_by and lag on the below dataframe ID DATE Value 555 1/9/2018 10 555 2/9/2018 20 555 3/9/2018 50 555 4/9/2018 70 000 1/9/2018 0 000 2/9/2018 …
Zidane Ahmed
  • 81
  • 1
  • 3
0
votes
2 answers

R how to change the value of a variable based on a condition using dplyr::mutate and if_else?

I'm trying to change a value of a variable in a data.frame where if a condition is met, then the variable takes another value, and if the condition is not met, the variable takes its original value. I'm confused why I'm getting an error and would…
Amazonian
  • 307
  • 6
  • 19
0
votes
0 answers

Conditional replacement r across columns. gsub vs. mutate_all?

I'd like to change certain characters across columns to a different character. For instance, mockdata<- data.frame(a = paste(rep(letters[1:4],10), rep(1:2, 20), sep = ""), b = paste(rep(letters[4:1],10), rep(1:3, len= 40), sep = "")) Here, I'd like…
ffew
  • 13
  • 3
0
votes
1 answer

Logstash Field split and merge

I started using and configuring ELK stack a month ago as a personal project in the IT company I'm working in. Without any training or a coding background, my Logstash works good enough to accept Logs from Cisco ASR5K on StarOS and some IOS devices,…
0
votes
1 answer

Mutate using Dply depeding upon the condition

I have create marks of 15 students for 5 subjects. I have also create a vector of passmarks in each subject. I want to mutate each column of marks with Pass or Fail depending upon the values given in the vector passmarks... I have been able to…
0
votes
2 answers

r - dplyr newbie - group_by

I have a dataset with 20 variables and 250K rows. I would like to add new variable, "NumAdms", based on the number of rows in "OT_entry" per individual "patient_id". I have constructed a dummy example: library(dplyr) reproeg <-…
0
votes
1 answer

R - Create (Mutate) a new column as a function of past observations

Ok so i have a pretty large data set of around 500 observations and 3 variables. The first column refers to time. For a test data set I am using: dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10, 1,1.8,3.5,3.8,5.6,6.2,7.8,8.2,9.8,10.1, …
0
votes
2 answers

Using tidyverse to get raw_alpha values with the psych package from different datasets / databases

I spent one day looking for this answer and I'm almost giving up. Actually, I really imagine is a pretty simple situation, but I'll be glad of any help. Let's say I have two datasets, the first get all ID of all…
Luis
  • 978
  • 7
  • 21
0
votes
1 answer

Create lags relative to whole change within group

I've tried creating a variable that represents the lagged version of another variable relative to the whole change of the variable within the group. Let's use this example dataframe: game_data <- data.frame(player = c(1,1,1,2,2,2,3,3,3), level =…
Scijens
  • 453
  • 2
  • 9
0
votes
2 answers

error when using NSE (in dplyr) : object 'value' not found

I'm trying to get familiar with using NSE in my code where warranted. Let's say I have pairs of columns and want to generate a new string variable for each pair indicating whether the values in that pair are the…
lost
  • 1,144
  • 6
  • 13
0
votes
1 answer

Use case_when function of dplyr with connection between two datasets

I have the following two example datasets and want to use case_when function as a combination of the two datasets: game_data <- data.frame(player = c(1,1,1,2,2,2,3,3,3), level = c(1,2,3,1,2,3,1,2,3), score=c(0,150,170,80,100,110,75,100,0)) >…
Scijens
  • 453
  • 2
  • 9
0
votes
1 answer

Replace values in Tidy Table

I have ta table with count data: I am trying to remove all non-zero values with one. This is what I have tried: div.summary<- raw.data %>% select (Site, 5:55) %>% group_by(Site) %>% summarise_all(sum) %>% mutate_if(replace(2:52, >0, 1))
I Del Toro
  • 833
  • 2
  • 12
  • 33