Questions tagged [mutate]

426 questions
2
votes
2 answers

R - mutate getting the first smaller value from the top rows

So, the problem is, I'm trying to get the first smaller value within the top rows of the transformed variable. My df looks something like: count 24 33 33 34 35 33 34 35 ... It only contains a column. The output that I'm…
2
votes
1 answer

How to calculate days between positive and negative results and creating new columns for each with R

I have looked for answer to this but I have not been able to find one that will help me with a column that has numbers and strings. My data look like this Name <- c("Doe, John","Doe, John","Doe, John", "Doe, Jane", "Doe, Jane","Doe, Jane","Parker,…
T.McMillen
  • 115
  • 7
2
votes
0 answers

Go Gota: Getting "wrong dimensions" error while running "mutate" to create a new col

I have a dataframe df that has two columns, i.e. A and B. I am trying to create a new column and adding it to the df like below: logFunc := func(s series.Series) series.Series { floats := s.Float() outCol := []float64 {} for _, f :=…
Axas
  • 21
  • 3
2
votes
3 answers

set column value to first value meeting conditions across columns

I have a dataframe with columns that start with "dx". I want to create another column called primary. If the value in all of the columns that start with "dx" is "I629" or NA, I want the value of primary to be "Unspecified". Otherwise, I want the it…
ltong
  • 501
  • 1
  • 8
2
votes
3 answers

How to create a new df with each "cell" mutated by a formula?

I have a dataset from which I am trying to get the relative abundances. I would like to be able to create a new df in which each "cell" has been transformed as such- new value=(original value)/(sum of column) My dataset has several hundred columns…
salix7
  • 61
  • 5
2
votes
1 answer

Nested ifelse in mutate produce the wrong output

I am trying to create a new colum depending on existing column. I want a column infected = 1, when there is at least 1 positive case (meaning nb_positive>0), infected=0 if nb_positive=NA and nb_negative>0 and infected =NA if nb_positive=NA and…
2
votes
1 answer

How to conditionally select a column, and subtract values in those rows from rows in another conditionally selected column in R?

I understand that my question title is quite wordy and may be difficult to understand so I'll try my best to explain it here. Here is some example data: transactions <- tibble(id = seq(1:7), day = paste(rep("day", each = 7),…
kiwi
  • 565
  • 3
  • 11
2
votes
2 answers

How to add a column to a dataset which adds values from one column and subtracts values from another column in R?

Here is a small example of the kind of data I have: transactions <- tibble(id = seq(1:7), day = paste(rep("day", each = 7), seq(1:7), sep = ""), sent_to = c(NA, "Garden Cinema", "Pasta House", NA, "Blue…
kiwi
  • 565
  • 3
  • 11
2
votes
1 answer

I want to use a previously created function in the mutate() function. Yet R doesn't seem to want to let me

I am looking at population data and want to make sure I have enough observations do to county level analysis. Therefore I would like to generate a variable that assigns each observation the number of observations with the same value for the "county"…
Tilman
  • 31
  • 3
2
votes
2 answers

Using group_by() to collapse a dataset in R based on conditions

I am trying to collapse a dataset based on conditions and groupings from another dataset. My current dataframe looks like this For every 'RollNo' in every 'congress' I want a new variable indicating if the two senators in the same state voted…
2
votes
1 answer

tidyr mutate new column based on group by with calculation

Using tidyr, how can I create a new column through a group-by and calculation? For example, if I have this dataframe: name <- c("a", "a", "a", "a", "b", "b", "b", "b") x1 <- c(0, 0, 0, 0, 1, 1, 1, 1) x2 <- c(15, 15, 15, 15, 15, 15, 15, 15) y <- c(1,…
a11
  • 3,122
  • 4
  • 27
  • 66
2
votes
2 answers

Using for loops with mutate function?

I have a task that's becoming quite difficult for me. I have to create a variable (pr_test_1) to test whether a variable for a procedure (I10_PR1) is in a list of procedures, and this code is working great: df <- df %>% …
2
votes
4 answers

how to find the difference from each group-value from the maximum within one group in R?

I have the following data: data <- data.frame(id_pers=c(1, 2, 3, 4, 5), Birthyear=c(2018, 2009, 2008, 2000, 1998,2005), family=c(Elliot, Elliot, Elliot, Gerrard, Gerrard,Gerrard) I…
Max Herre
  • 47
  • 5
1
vote
1 answer

Mutate across for multiple columns - R

I have a df that has imperfect data in a number of columns. I do not wish to delete those columns but rather set each value in all of those columns to NA so that I can still have them in the data frame. I am using dplyr and am aware that I could…
1
vote
5 answers

Create multiple columns with R dplyr mutate with across instead of with a loop?

I am trying to use R's dplyr package to create multiple new columns for each year in my dataset that is the sum of the columns corresponding to each year's end of quarter figures (Mar, Jun, Sep, Dec). The only way I have been able to figure out how…
StatsStudent
  • 1,384
  • 2
  • 10
  • 28
1 2
3
28 29