Questions tagged [mutate]

426 questions
0
votes
3 answers

In dplyr::mutate, refer to a value conditionally, based on the value of another column

Apologies for the unclear title. Although not effective, I couldn't think of a better way to describe this problem. Here is a sample dataset I am working with test = data.frame( Value = c(1:5, 5:1), Index = c(1:5, 1:5), GroupNum = c(rep.int(1,…
Tee
  • 149
  • 8
0
votes
0 answers

Mutate columns in r by calculating the distance of longitude and latitude

I'm trying to solve a problem using r I have four columns in my dataframe, start_lat, end_lat, start_lng, end_lng and I want to create a new column called ride_distance measuring the difference between the 4 columns. How do I go about this please…
0
votes
0 answers

substract data if character appear in rows (R)

I have the following data frame: > str(Long_top) 'data.frame': 110 obs. of 6 variables: $ Category : chr "GOTERM_BP_DIRECT" "GOTERM_CC_DIRECT" "GOTERM_MF_DIRECT" "GOTERM_CC_DIRECT" ... $ Term : chr "GO:0016579~protein…
Chemokine1
  • 17
  • 3
0
votes
1 answer

Loop to recode variables using dplyr mutate recode

I am new to R, and I have been using Stata for multiple years. I am trying to create a loop and a set of conditions within the loop that will do some recoding of data. I am, truly, lost, so any suggestions are welcomed. Please,…
George
  • 95
  • 7
0
votes
1 answer

Creating a new column using a condition and loop

let's assume I have a dataset similar to this: df1 <- data.frame(age==1:10, year==2004) df2 <- data.frame(age==1:10, year==2005) df3 <- data.frame(age==1:10, year==2006) df <- rbind(df1,df2,df3) df <- df %>% mutate(group=case_when(age %in% c(1:3) &…
Y.H.
  • 9
  • 1
0
votes
1 answer

Changing value in R based on multiple conditions

I have a dataframe with the below fields and values: | team_home | team_away | team_favorite_id | |:------------:|:----------:| ----------------:| | Kansas City | Baltimore | KC | I needed to change the team_favorite_id to match…
Abartel
  • 21
  • 4
0
votes
1 answer

R Create Multiple New Variables

I'm having trouble finding a solution for a complex variable assignment need. I need to create new variables based on the values in existing variables, but I need to do the same new variable creation many times using a different set of existing…
lemnbalm
  • 5
  • 3
0
votes
0 answers

Trying to create a new column using case_when in R

This df is from the complete journey package. have join df's in order to show the transaction_timestamp, household_id, product_type and product_kind). Product kind is a variable created to show what type of item bought was. The problem is, each…
0
votes
0 answers

Trouble recreating R code that uses case_when with the replace() function

I am trying to familiarize myself with the different functions and R and ran into some difficulties recreating the following code using the replace() instead of case_when(). The packages I am using are: library(tidyverse) library(nycflights13) The…
0
votes
0 answers

Idiomatic workarounds or alternatives to the mutate after immutable borrow problems in Rust

One of the main features of Rust is the borrow checker. Now I found a problem that I cannot solve because I need to mutate after immutable borrow. I am looking for workarounds or alternative solutions for what I want to do, preferably idiomatic…
exocortex
  • 375
  • 2
  • 9
0
votes
0 answers

Create a new variable from multiple variables

I have a dataset with patients undergoing radical prostatectomy. Variables starting with ANM_1_surgery identify patients who had surgery before prostatectomy, and their value is 0/1 I want to create a new variable called prev_surg, with values…
simone.s
  • 1
  • 1
0
votes
2 answers

How to create a function to do an operation on multiple columns to create new columns in R

I'm trying to apply a simple operation to multiple columns in a dataset. I tried using a function like the one explained in Applying function on multiple columns to create multiple new columns, but I haven't been able to get it. here is the…
Cae.rich
  • 171
  • 7
0
votes
1 answer

Creating a column based on two criteria using max values from another column

I've got a dataset of species observations over time and I am trying to calculate observation dates based on the max value of criteria: Df <- data.frame(Sp = c(1,1,2,2,3,3), Site = c("A", "B", "C", "D"), date =…
RGR_288
  • 51
  • 6
0
votes
0 answers

Use ifelse to fill in NAs with value from other column in R

I have a dataset with 7 columns and I am trying to make an 8th column based on conditions of 2 other columns. I've managed to calculate the first condition (take the last date before an NA), but I am not trying to fill in the row that does have an…
Cae.rich
  • 171
  • 7
0
votes
2 answers

Trying to mutate the state of React component

I'm trying to create a quizz app and got stuck at the moment when I need to change the background of the Answers (button) when the user clicked on it. Well, function holdAnswer does console.log the id of the answer, but doesn't change its…
lsokol
  • 15
  • 4