Questions tagged [rowwise]
229 questions
44
votes
3 answers
Efficient row-wise operations on a data.table
I need to find the row-wise minimum of many (+60) relatively large data.frame (~ 250,000 x 3) (or I can equivalently work on an xts).
set.seed(1000)
my.df <- sample(1:5, 250000*3, replace=TRUE)
dim(my.df) <- c(250000,3)
my.df <-…

Ryogi
- 5,497
- 5
- 26
- 46
38
votes
5 answers
data.table row-wise sum, mean, min, max like dplyr?
There are other posts about row-wise operators on datatable. They are either too simple or solves a specific scenario
My question here is more generic. There is a solution using dplyr. I have played around but failed to find a an equivalent solution…

Polymerase
- 6,311
- 11
- 47
- 65
16
votes
2 answers
R return the index of the minimum column for each row
I have a data.frame that contains 4 columns (given below). I want to find the index of the minimum column (NOT THE VALUE) for each row. Any idea hiw to achieve that?
> d
V1 V2 V3 V4
1 0.388116155 0.98999967…

Vahid Mirjalili
- 6,211
- 15
- 57
- 80
11
votes
3 answers
dplyr rowwise sum and other functions like max
If I wanted to sum over some variables in a data-frame using dplyr, I could do:
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 …

mjandrews
- 2,392
- 4
- 22
- 39
8
votes
7 answers
Problem using rowwise() to count the number of NA's in each row of a dataframe
I'm having trouble using rowwise() to count the number of NAs in each row. My minimal example:
df <- data.frame(Q1 = c(rep(1, 1), rep(NA, 9)),
Q2 = c(rep(2, 2), rep(NA, 8)),
Q3 = c(rep(3, 3), rep(NA, 7))
)
df
Q1…

Thomas Philips
- 935
- 2
- 11
- 22
7
votes
4 answers
How to use dplyr to get column with max value for each row
I have a dataframe in R. For each row, I would like to select which column has the highest value, and paste the name of that column. This is simple when there are only two columns to chose from (note that I have a filtering step that doesn't…

icedcoffee
- 935
- 1
- 6
- 18
7
votes
4 answers
Combine: rowwise(), mutate(), across(), for multiple functions
This is somehow related to this question:
In principle I try to understand how rowwise operations with mutate across multiple columns applying more then 1 functions like (mean(), sum(), min() etc..) work.
I have learned that across does this job and…

TarJae
- 72,363
- 6
- 19
- 66
7
votes
2 answers
row-wise operations, select helpers and the mutate function in dplyr
I will use the following data set to illustrate my questions:
my_df <- data.frame(
a = 1:10,
b = 10:1
)
colnames(my_df) <- c("a", "b")
Part 1
I use the mutate() function to create two new variables in my data set and I would like to compute…

SavedByJESUS
- 3,262
- 4
- 32
- 47
6
votes
1 answer
Row wise matrix operations in R
Recently I ran into the data.table package.
I'm still not sure how to do row-wise matrix operations.
Was it originally intended to handle such operations?
For example, what would be data.table equivalent to apply(M,1,fun)?
fun should take a vector…

danas.zuokas
- 4,551
- 4
- 29
- 39
5
votes
3 answers
data.table sample with probabilities stored in columns
I have a data table with probabilities for a discrete distribution stored in columns.
For example, dt <- data.table(p1 = c(0.5, 0.25, 0.1), p2 = c(0.25, 0.5, 0.1), p3 = c(0.25, 0.25, 0.8))
I'd like to create a new column of a random variable sampled…

Gregory Apartment
- 51
- 1
5
votes
6 answers
Deal with missing data in row-wise manipulation
ID <- 1:6
math <- c("YES","NO","YES","NO",NA,NA)
history <- c(NA,NA,"NO","NO","YES",NA)
dt <- data.frame(ID, math, history)
ID math history
1 1 YES
2 2 NO
3 3 YES NO
4 4 NO NO
5 5 YES
6 6 …

yoo
- 491
- 3
- 10
5
votes
2 answers
How to do parallel processing with rowwise
I am using rowwise to perform a function on each row. This takes a long time. In order to speed things up, is there a way to use parallel processing so that multiple cores are working on different rows concurrently?
As an example, I am aggregating…

bill999
- 2,147
- 8
- 51
- 103
5
votes
3 answers
Find minimum value in an array that is larger than another column in R
I need to find the minimum values of three columns that are bigger than the values in another column. Say these five individuals entered a hospital in different months of the year, and they suffered several heart attacks before and after…

elliezee
- 113
- 4
5
votes
2 answers
Preferred performant procedure for R data.table row-wise operations?
Does the following code represent the preferred procedure for traversing the rows of an R data.table and passing the values found at each row to a function? Or is there a more performant way to do this?
library(data.table)
set.seed(2)
n <- 100
b <-…

t-student
- 414
- 2
- 16
5
votes
1 answer
Using pmap to iterate over rows of a tibble
I have a very simple tibble and I would like to iterate over its rows to apply a function using pmap function. I think I may have misinterpreted some points on pmap function but I mostly have difficulty selecting arguments. So
I would like to know…

Anoushiravan R
- 21,622
- 3
- 18
- 41