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 plot semiannual data evenly?

I have a dataset of dates and stock returns. I am trying to create a line plot returns by semiannual periods. I mutated my dates to semiannual periods using dataTable <- dataTable %>% mutate(Semiannual = semester(Date, with_year = TRUE)) See a…
figNewton
  • 107
  • 1
  • 7
0
votes
1 answer

How to fix factor level order at the time of variable creation in R

I am creating a set of variables all having the same levels: 0 or 1. However when I print them sometimes the table starts with value 0 and sometimes with 1. I'd like to see if I can lock the order when creating them. Here is how they are…
Ana
  • 1,132
  • 3
  • 12
  • 20
0
votes
1 answer

Clustering rows by ID based on a column value condition multiple times

Some time ago I opened a related question in this post Suppose I have the following df: data <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,1, 1, 1,1,1,1,1,1,1,1,1,1), Obs1 = c(1,1,0,1,0,1,1,0,1,0,0,0,1,1,1,1,1,1,1,1,0,1), …
torakxkz
  • 471
  • 4
  • 14
0
votes
0 answers

Mutate vector by keeping state of the last, newly created, value

I have a data frame like this: # A tibble: 14 x 1 inventory 1 3 2 4 3 NA 4 5 5 NA 6 NA 7 NA 8 NA 9 10 10 14 11 NA 12 NA 13 NA 14 16 I want to…
Pomelo
  • 1
  • 1
0
votes
1 answer

Using custom mutate function in dplyr

I am new to R and a function that creates the first difference at different lag values (see code below). dt %>% arrange(GEO, LF.char, NAICS, new_date) %>% mutate(fd = VALUE -lag(VALUE), FD_percent = fd/lag(VALUE), fd3 =…
Jrakru56
  • 929
  • 5
  • 14
0
votes
2 answers

R - mutate condition in huge data.frame

So I have very large dataset (>1000 obs. of >15000 variables) and I wan't to replace all values >1 with 1 and leave the rest unchanged. Example data: data <- data.frame(a = 1:10, b = -1:-10, c = letters[1:10]) a b c 1 1 -1 a 2 2 -2 b 3…
ceefel
  • 143
  • 1
  • 10
0
votes
1 answer

How to add a new column based on rows with a pattern and a sequence in R?

If a have a dataframe with a column with a pattern that is: a row with a string with a name on it followed by other rows containing names and a sequence of numbers. This is repeated all over the dataframe. I want to create a new column base on the…
ronnyhdez
  • 21
  • 5
0
votes
2 answers

Select from column in dataframe based on value in another column

I have a dataframe as follows: dataDF <- data.frame( id = 1:5, to_choose = c('red', 'blue', 'red', 'green', 'yellow'), red_value = c(1,2,3,4,5), blue_value = c(6,7,8,9,10), yellow_value =…
user1165199
  • 5,057
  • 12
  • 40
  • 57
0
votes
3 answers

Creating new columns with mutate

i can figure out the solution of my problem but in a very not optimal way and thus the solution i have is not adapted for a large df. Let me explain. I have a big dataframe and i need to create new columns by subtracting two others ones. Let me…
F. Lyon
  • 55
  • 3
0
votes
1 answer

Create a new variable and assign the value to a group

I have a tibble of 65524 observations where one variable is an ID for an household and the other is factor where the value of 1 is assigned if the age of the person in the household is less then 15y.o., 2 is assigned if the age is between 15 and 64,…
Manolo
  • 63
  • 7
0
votes
1 answer

How to populate a column based on any() in another column in a grouped data.frame?

I have a dataframe jj: jj<-data.frame(a=rep(1:3,3),b=c(TRUE,rep(FALSE,4),TRUE,TRUE,FALSE,TRUE)) I want to create a third column that will be true if any of the rows with a particular a have b==TRUE. So I tried this: group_by(jj,a) %>%…
iod
  • 6,861
  • 2
  • 13
  • 30
0
votes
1 answer

How can I use v-model and without mutating the value in VueJS?

I have an input that uses the v-model directive to control the data. I need to watch said data and figure out if it's been changed. When I watch the data, the new and old values are the same. This is because the data is mutated, as seen in the…
Gurnzbot
  • 2,190
  • 3
  • 25
  • 44
0
votes
2 answers

mutate column based on conditions, preserving original content

How would I go about setting a conditional mutate on a column, but preserving the original content. For example, consider: DF=data.frame(A=1:10, B=c("fail1", "fail2", rep(NA,8))) DF=DF %>% mutate(B = ifelse(A<4,paste(B, "less than 4"),B)) What I…
sym246
  • 1,506
  • 2
  • 16
  • 41
0
votes
1 answer

Error in mutate_impl(.data, dots) on lists

I have a dataframe and I would like to add a column with matched patterns which I have stored in a list, something like this: This is my dataframe: order_lines <- tibble( order_number = c(100, 200, 300, 400, 500, 600), description = c("xyz",…
0
votes
1 answer

How to simultaneously read in excel sheets and mutate a new column with purrr/dplyr?

I'm trying to read in a time series dataset spread over multiple years (so the sheet names are the respective years). I want to read in each sheet and then mutate a new column called "year" that's equal to the sheet name. I'm not sure how to do…
Simon
  • 15
  • 4