Questions tagged [mutate]

426 questions
1
vote
2 answers

Create a new variable of concatenated values of other columns using dplyr::mutate and a vector of choice columns

I have this dataframe: vehicles <- tibble(vehicle = c("Car", "Car", "Motorbike"), color = c("Red", "Black", "Blue"), speed = c("Low", "High",…
1
vote
2 answers

how can i group_by NA's as well?

with this formula: datanew <- df_bsp %>% group_by(id_mother) %>% dplyr::mutate(Family = cur_group_id()) I got this output: datanew <- data.frame(id_pers=c(1, 2, 3, 4, 5, 6), id_mother=c(11, 11, 11, 12, 12, 12), …
Max Herre
  • 47
  • 5
1
vote
0 answers

decimal places in mutate results in R

I am building a shiny app, and i have difficulties fixing the decimal places in mutate results. What i need is that, to add comma in the GDP.Scenario & Job.Scenario, meaning that the numbers appear as 1,000,000 for example. The code and the Shiny…
Ebtihaj
  • 19
  • 1
1
vote
1 answer

How to iteratively mutate every column in a tibble based on an operation with other tibble?

I have the following data frame: library(tidyverse) dat <- structure(list(residue = c("A", "R", "N"), PP1 = c(-0.96, 0.8, 0.82), KF2 = c(-1.67, 1.27, -0.07)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame")) It looks like this: >…
neversaint
  • 60,904
  • 137
  • 310
  • 477
1
vote
1 answer

combine redundant row items in r

I have a dataset with the the names of many different plant species (column MTmatch), some of which appear repeatedly. Each of these has a column (ReadSum) with a sum associated with it (as well as many other pieces of information). How do I…
salix7
  • 61
  • 5
1
vote
0 answers

Modify dataframe values with mutate in R

I have a dataset that has only one variable v. I want to calculate for every new row the previous cumulative max value of v (maxprix) and the previous cumulative standard deviation (sdd). For every row I want to test if the v is bigger than maxprix…
1
vote
2 answers

Modify data frame with condition based on another data frame in R

I have two data frames: df1 <- data.frame(Q1 = rep(c(0, 1), 3), Q2 = rep(c(1, 0), 3), Q3 = rep(1, 6)) df2 <- data.frame(Q1 = rep(c(0, 1, 2), 2), Q2 = rep(c(2, 1, 0), 2), Q3 =…
1
vote
3 answers

How to find the sum of two consecutive rows of a column from a dataframe based on a condition of another columns(using,maybe, dplyr's mutate function)

I have the following data frame (say df1) in R: and would like to find the sum of the sizes for each of the "continent", "country", and "sex" combinations in cases where the category column values are A and B. I would then like to change the…
1
vote
1 answer

How to create factor with mutate with levels coming from another col

I have this df: city code PR 41 SC 42 RS 43 I want to change city to factor, but keep the order as in the code. If I only do df %>% mutate(city= factor(city)) it orders alphabetically. I would like something such as df %>%…
uow
  • 120
  • 7
1
vote
1 answer

Collect column names from all none-zero rows into a new column

Using the same data set in fill a column with name and content of 3 other columns dput(df) structure(list(trtmt = c(26, 28, 35, 42, 49, 63, 33, 24, 38, 4, 24, 10, 59, 20, 16, 15, 66, 23, 51, 41, 39, 17, 8, 9, 28, 2, 13, 32, 43, 58, 4, 49, 63, 27,…
hnguyen
  • 772
  • 6
  • 17
1
vote
2 answers

fill a column with name and content of 3 other columns

dput(df) structure(list(trtmt = c(26, 28, 35, 42, 49, 63, 33, 24, 38, 4, 24, 10, 59, 20, 16, 15, 66, 23, 51, 41, 39, 17, 8, 9, 28, 2, 13, 32, 43, 58, 4, 49, 63, 27, 27, 34, 6, 8, 57, 43, 23, 10, 50, 47, 41, 65, 57, 21, 15, 35), R = c(150, 150, 0,…
hnguyen
  • 772
  • 6
  • 17
1
vote
1 answer

Mutate across - returning values from columns which number is stored in another ones?

I have a following dataset with 100 variables Z1 to Z100 containing character values and 10 variables containing column numbers. Z1 ... Z100 V1 ... V10 NA ... xyz 56 ... NA An exemplary dataset - with 5 rows, 5 Z-variables (Z1-Z5) and 3…
1
vote
0 answers

Error in `bind_rows()`: ! Can't combine `..1$cyl` and `..2$cyl` .?

I am trying to reproduce and understand a colleague's code in my system library(tidyverse) data <- mtcars tabl1 <- data %>% mutate(cyl = as.character(cyl)) %>% bind_rows(mutate(., cyl = "all")) %>% group_by(cyl) %>% summarise(mpg_q25 =…
Varun KV
  • 11
  • 4
1
vote
2 answers

dplyr mutate based on columns condition and an external vector

I am trying to add a list column to a tibble data frame. The resulting list column is calculated from two columns contained in the data frame and a vector which is external / independent. Suppose that the data frame and the vector are the…
1
vote
1 answer

How do you count the number of observations in multiple columns and use mutate to make the counts as new columns in R?

I have a dataset that has multiple lines of survey responses from different years and from different organizations. There are 100 questions in the survey and people can skip them. I am trying to get the average for each question by year by…
vp2019
  • 29
  • 4