Questions tagged [rowwise]
229 questions
2
votes
1 answer
Loop through rows and change values when meeting a condition
Sample data:
df <- data.frame(bucket = c("A", "B", "C", "D"),
apples = c("<4", "U", "22", "9"),
oranges = c("U", "<4", "15", "7"),
bananas = c("8", "6", "16", "<4"),
pears = c("U",…

k3b
- 344
- 3
- 15
2
votes
4 answers
Alternative (faster) approach to dynamically create row means for multiple groups of columns
I'm trying to automatically calculate the mean score per row for multiple groups of columns.
E.g. a set of columns could represent items of different scales.
The columns are also systematically named (scale_itemnumber).
For example, the dummy data…

Rasul89
- 588
- 2
- 5
- 14
2
votes
2 answers
R rowwise replace the first instance of the minimum
How can I do the following:
replace all values < 6 with NA,
if there is only one NA in the row, replace the first instance of the minimum value with -99?
Some data that includes an ID variable and a total column:
library(tidyverse)
df <-…

m.tripley
- 23
- 4
2
votes
2 answers
doing a lookup function between rows and columns in r
I have a dataframe that has a column, called ID in dummy data below, that has values that match to 1 of a few subsequent column names. I want to do some sort of lookup function, where it sees the value of ID, finds the matching column, and then…

tchoup
- 971
- 4
- 11
2
votes
3 answers
Interpolate row-wise values of 0 between two columns with values >0 in R
I try to interpolate values of 0 between two values unequal to zero row-wise for the columns: 2018 to 2021 of a data.table in R. This is how a sample data df1 would look like:
ID string1 2018 2019 2020 2021 string2
1: a1 x2 3 3 0 …

fjurt
- 783
- 3
- 14
2
votes
2 answers
Is there a way to use rowwise to get means across rows the correct way?
Am not sure whether I have fully understood the rowwise function in dplyr. I seem to get the expected results. Below is the code and the expected results.
library(dplyr)
set.seed(123)
mydf <- tibble(
a1 = floor(rnorm(10, 5, 2)),
a2 =…

Moses
- 1,391
- 10
- 25
2
votes
2 answers
Row wise normality, skewness and kurtosis in one command
My data is:
X0 X1 X2 X3 category
0 15 4 4 TAH
0 2 5 0 MAT
0 11 9 0 BIO
I want to calculate row-wise normality, skewness and kurtosis. The main reason is that I have categories over different rows (in a dedicated column). Is there a…

Sandy
- 1,100
- 10
- 18
2
votes
2 answers
Is there a dplyr way to generate an AR(1) time series?
I need to generate an autoregressive timeseries for a simulation study. Ultimately I will need to do this in a nested fashion over many blocks. I thought I could easily achieve this using mutate() and lag() but apparently not:
# Initialise some…

sometimes_sci
- 183
- 1
- 10
2
votes
3 answers
tidyverse combine a lagged row value + certain character if condition is met
Assume the following data:
set.seed(1)
df <- data.frame(name = rep(letters[1:3], 5),
condition = sample(0:1 , 15, replace = TRUE))
df
name condition
1 a 0
2 b 1
3 c 0
4 a 0
5…

deschen
- 10,012
- 3
- 27
- 50
2
votes
1 answer
`purrr` alternative to row-wise function that determines event date based on complex rule set
I am working with a client that wants to provide an input spreadsheet with a text description of when certain events should occur in a given year. Each event (and there are at least 200 of them) is a separate row, containing a complex rule set about…

Nova
- 5,423
- 2
- 42
- 62
2
votes
3 answers
Euclidean distant for NON-CONSECUTIVE classes of factors iterated by groups
This question is an extension of this question.
Euclidean distant for distinct classes of factors iterated by groups
The same explanations from the previous question apply here as well. I want to calculate the Euclidean distance between consecutive…

lovestacksflow
- 521
- 3
- 14
2
votes
1 answer
Euclidean distant for distinct classes of factors iterated by groups
*Update: The answer suggested by Rui is great and works as it should. However, when I run it on about 7 million observations (my actual dataset), R gets stuck in a computational block (I'm using a machine with 64gb of RAM). Any other solutions are…

lovestacksflow
- 521
- 3
- 14
2
votes
5 answers
How to create a column based on other columns in a vectorized/fast fashion?
I need to create one column which is base on other two columns. The thing is I don't want to use rowwise because it makes it too slow when used on large data, when I don't use rowwise it does not work.
library(tidyverse)
df <- tibble(
param1 =…

Alvaro Morales
- 1,845
- 3
- 12
- 21
2
votes
1 answer
Is there a way to combine across() and mutate() if I am referencing column names from a list?
The dataset below has columns with very similar names and some values which are NA.
library(tidyverse)
dat <- data.frame(
v1_min = c(1,2,4,1,NA,4,2,2),
v1_max = c(1,NA,5,4,5,4,6,NA),
other_v1_min = c(1,1,NA,3,4,4,3,2),
other_v1_max =…

Kim
- 21
- 1
2
votes
2 answers
How to check if values in individiual rows of a data.table are identical
Suppose I have the following data.table:
dt <- data.table(a = 1:2, b = 1:2, c = c(1, 1))
# dt
# a b c
# 1: 1 1 1
# 2: 2 2 1
What would be the fastest way to create a fourth column d indicating that the preexisting values in each row are all…

johnny
- 571
- 4
- 14