Questions tagged [mutate]

426 questions
1
vote
0 answers

R tidyverse: mutate raw scores to add t-scores using an existing validated conversion table

I have a raw test score on n subjects, and a previously validated conversion table that links each possible raw score to a t-score and an SE. The goal is to assign the correct tscore and SE to each subject. The following produces the desired outcome…
Gitta
  • 15
  • 2
1
vote
1 answer

mutate_if multiple conditions along with case_when in R

I want to apply the case_when function over those columns who are factors and have only 2 levels and whose names do not contain "kiwi". This does not work. I can get what I want with other longer approaches but I was wondering if there is any way to…
Mathica
  • 1,241
  • 1
  • 5
  • 17
1
vote
2 answers

How can I replace unique values above 0 with numbers starting from 1 within a group in R?

I have the following dataframe. And I need to replace unique values above 0 with numbers starting with 1 within each group? I understand how to replace values within a column or within a row but not within a whole group. df <- data.frame( group =…
1
vote
2 answers

Get maximum value conditional on another value which might be missing

I'd like to get the maximum date within a group where another column has a specific value. In the specific example, I'd like to extract the maximum date of all dates within a group where status == "inactive". If there are no dates where…
Johannes
  • 1,024
  • 13
  • 32
1
vote
2 answers

Flagging Outliers using while loop and dynamic names along with mutate R

I need to mark columns containing outliers in R -- preferably using a while loop for so its easy to apply to other situations. I would like to create a new variable for each column denoting if the outlier is in the greater than or lower than IQR…
Englishman Bob
  • 377
  • 2
  • 13
1
vote
1 answer

R function for transforming comma-separated values in a cell into multiple rows with same row name?

I have a two-column dataframe in R: the first column is a broad category, and the second column contains comma-separated items within the broad category. This is what it looks like: Orthogroup Sequences 0 Seq1, Seq2, Seq3 1 Seq4 And…
1
vote
1 answer

Count the number of timestamps in a given vector that fall within an interval in R

I want to count the number of events that occur within intervals. I start with a table that has three columns: start dates, end dates, and the interval created by them. table <- tibble( start = c( "2022-08-02", "2022-10-06", "2023-01-11"), …
dcossyleon
  • 115
  • 2
  • 7
1
vote
1 answer

Unknown levels in `f`:() but the columns are already factors

I've seen many posts concerning the warning Unknown levels in `f`: , however, all of them concerned the fact that the variable wasn't a factor before using fct_collapse(). All my variables are already factors, but I'm getting this warning…
Larissa Cury
  • 806
  • 2
  • 11
1
vote
1 answer

How to subtract the value of some rows from other rows in R according to categorical variables

I am trying to determine the number of verbs (n) in each tense and aspect (TA) for each text (filename) in my dataset. I have my data saved as a tibble (see below), but the values in the n column are not yet accurate because some categories subsume…
LilyLew
  • 25
  • 5
1
vote
2 answers

New variable conditional on whether a df1 column value equals any value included in a specific df2 column

I am trying to make a new variable using mutate() . In df1, I have ranges of values in col1, col2, col3, and col4. I would like to create a new binary variable in df1 that is "1" IF any of the col1-4 values are found in a specific df2 column (let's…
1
vote
4 answers

How to create new dataframe column with a character that comes from three other columns but only if present in a list, in R

I've been trying to do this with mutate(), str_detect(), but it's too complex and dont know how to do all the steps, please help! I have a dataframe in which 3 cols contain let's say fruits, animals, or…
Pame
  • 37
  • 4
1
vote
2 answers

Replacing values in one table from a corresponding key in another table by specific column

I am processing a large dataset from a questionnaire that contains coded responses in some but not all columns. I would like to replace the coded responses with actual values. The key/dictionary is stored in another database. The complicating factor…
sk454
  • 25
  • 3
1
vote
1 answer

Add a column at specific position using mutate in r

I want to add a column at specific position in r, but mutate (xx, .before = "exisiting column) doesn't work right: starwars %>% mutate (new_column = "Other", .before = 1) # A tibble: 87 × 16 name height mass hair_color …
Eleanor
  • 21
  • 3
1
vote
3 answers

Select column based on indicator in another column

I have a dataframe in R where I have a column with a numeric indicator, and I want to create a new column picking from other columns in the dataframe based on that indicator. Probably easiest if I explain via an example... data <- data.frame( id…
user1165199
  • 6,351
  • 13
  • 44
  • 60
1
vote
1 answer

dplyr mutate sequence by n levels of grouping by variable

So, my data looks like the following: Period Date 1 01-01-2020 1 02-01-2020 1 03-01-2020 2 04-01-2020 2 05-01-2020 2 06-01-2020 3 07-01-2020 3 08-01-2020 3 09-01-2020 4 10-01-2020 4 11-01-2020 4 12-01-2020 The…