Questions tagged [mutate]

426 questions
1
vote
3 answers

select values based on column index

I am looking to add a column to my tibble that is selected from my data frame based on the column index. library(dplyr) question <- c("q1","q2","q3","q4") a <- c("no","no","yes","no") b <- c("yes","yes", "no","no") c <-…
AdrieStC
  • 389
  • 4
  • 13
1
vote
4 answers

R Replace column values in dataframe base on matching indexing column in separate dataframe

I have a dataframe 'df1' that looks like: Number Variable1 Variable Variable3 1 A B C 2 A B C 3 A B C 4 A B C 5 A B C And I have a second dataframe 'df2' that looks…
Jacob
  • 329
  • 2
  • 10
1
vote
1 answer

create new column based on sum of another column and plot

I have a large data frame called data_frame with 3 columns PRE, STATUS, and CHR that look like this: PRE STATUS CHR 1_752566 GAINED 1 1_776546 LOST 1 1_832918 NA 1 1_842013 LOST 1 1_846864 GAINED …
1
vote
2 answers

Only keep the characters in front of "_" per row in a single column then create a new column with the output

I have a large data frame called data_frame with two columns PRE and STATUS that look like this: PRE STATUS 1_752566 GAINED 1_776546 LOST 1_832918 NA 1_842013 LOST 1_846864 GAINED 11_8122943 NA 11_8188699 GAINED 11_8321128 …
1
vote
2 answers

Use substrings of column names in across

I have the following tibble: df <- tibble( x = c(5, 5), Column_1 = c(0.5, 0.5), Column_2 = c(0.75, 0.75)) I would like to create two new columns, which are the product of column x and Column_1 and x and Column_2 respectively. These columns…
snowman
  • 33
  • 3
1
vote
1 answer

Count by group and condition on a number in R

I have the following dataset: id_municipio year Vivo Claro TIM Oi Algar Sercomtel MVNO 1 1100015 2009 65.4 13.0 28.2 90.8 0 0 0 2 …
Mateus Maciel
  • 151
  • 1
  • 1
  • 10
1
vote
1 answer

Mutate specific colums and add pattern to the name

I would like to use mutate to add new columns to a data.frame based on specific colums divided by another column and keep the originalname plus a fixed pattern. mtcars$mpg_HorsePower = mtcars$mpg / mtcars$hp mtcars$cyl_HorsePower = mtcars$cyl /…
candelas762
  • 137
  • 3
1
vote
1 answer

Dplyr - Using case_when (multiple conditions) with across

I'm trying to recode a large number of variables with 5 levels ("1_Disagree", "2_SomeD", "3_Neither", "4_SomeA", "5_Agree") into variables with 3 levels ("1_Disagree", "2_Neither", "3_Agree"). All these variables have similar names, so I'm using the…
Vetepi
  • 11
  • 2
1
vote
2 answers

r long to wide time series data with start and stop time columns

I like to reshape a time series data from long to wide format , with columns such as StartTime and StopTime. All variables measured during the same time interval (StartTime, StopTime) to be in the same line. For example if this is my dataset …
1
vote
1 answer

How to conditionally subtract values in one column from another conditionally selected column with duplicates, in R?

Here is some example data: transactions <- structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8), day = c("day1", "day2", "day3", "day4", "day5", "day6", "day7", "day8"), sent_to = c(NA, "Garden Cinema", "Pasta House", NA, "Blue Superstore", NA, NA, NA),…
kiwi
  • 565
  • 3
  • 11
1
vote
2 answers

Error handling function not working within dplyr::mutate

Why is R complaining about an error when my function already handles errors? I've created a function to grab the parent element of an href attribute which invariably is "". The function has some error handling to return NA if it can't find the…
Rez99
  • 359
  • 1
  • 4
  • 15
1
vote
1 answer

Summarize values between two rows, according to criteria

I have this dataframe my dataframe where values in the 'Age' columns need to be summarize per the whole size range i.e. now the data frame is like this: Size Age 1 Age 2 Age 3 [1] 8 2 8 5 [2] 8.5 4 7 …
1
vote
3 answers

code for mutate_at to recode values in a vector of column names

Im trying to write a code, that will take colnames out of a given vector, and in each of these columns recode 1<-5,2<-4,3<-3,4<-2,5<-1 big5qflip is the vectore of columns that I need to recode out of a questionnaire, I'm having trouble getting it…
aviv sion
  • 11
  • 2
1
vote
1 answer

Updating multiple date columns in R conditioned on a particular column

I have a table that consists of only columns of type Date. The data is about shopping behavior of customers on a website. The columns correspond to the first time an event is triggered by a customer (NULL if no occurrence of the event). One of the…
alexT
  • 49
  • 5
1
vote
1 answer

Trying to find conditional code that subtracts value from one variable from value in another variable, and inputs result into new variable

I want to run a code that checks value in variable x, and if conditions are met takes value from variable y and subtracts it from variable z, and inputs it into variable q. If conditions are not met then looks at other conditions and subtracts z…
Eli
  • 15
  • 3