Questions tagged [rowwise]

124 questions
13
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
  • 5,363
  • 15
  • 47
  • 73
9
votes
2 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,113
  • 3
  • 17
  • 35
6
votes
3 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
  • 8,026
  • 2
  • 8
  • 25
6
votes
1 answer

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
  • 2,471
  • 4
  • 22
  • 38
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
  • 302
  • 1
  • 14
4
votes
2 answers

rownames_to_column does not work after rowwise() properly

I have this df: df <- structure(list(a = 1:5, b = 6:10, c = 11:15, d = c("a", "b", "c", "d", "e"), e = 1:5), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame")) a b c d e 1 1…
TarJae
  • 8,026
  • 2
  • 8
  • 25
4
votes
6 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
  • 489
  • 7
  • 15
4
votes
1 answer

R {dplyr}: `rename` or `mutate` data.frames in `rowwise` list-column with different column names on LHS

I'm playing around with list-columns of data.frames with {dplyr} 1.0.0 and I'm wondering whether it is possible to rename() and mutate() columns in each data.frame without leaving the pipe when the nested data.frame is grouped rowwise. Why do I want…
TimTeaFan
  • 6,780
  • 2
  • 9
  • 23
4
votes
2 answers

Row-wise operations with mutate_at

Using dplyr, is there a way to selectively mutate columns by row without using rowwise()? For example, given input below, I want to replace negative numbers with zeroes in columns prefixed with "pre_": df <- data.frame(a=c(-5, 3, 4, 5), b=4:1,…
Megatron
  • 12,223
  • 9
  • 75
  • 86
3
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
  • 5,528
  • 7
  • 24
3
votes
4 answers

R programming, row-wise data frame calculation with custom script (for every i) to solve "bridge game"

I have a data frame which specifies "bridge games" (every row is one independent game), see a minimal example with 4 games below: start <- list(c("10","15","5"), c("5") ,c("11","6"),c("6","11")) end <- list(c("7","17","11"), c("10"),…
Quad89
  • 45
  • 5
3
votes
2 answers

Why does using pipes and map fail on a list of data frames?

I have tibbles nested in a list with an identifier column as well. I would like to run anonymous functions on each nested tibble. However, when I use a pipe to refer to my main df and then to the list that contains my data map does not work. #…
3
votes
3 answers

tidy: create key without rowwise()?

Is there a way to create a key without using rowwise()? Any pointer is much appreciated. df <- tibble(grp1=rev(LETTERS[1:5]),grp2=letters[11:15],grp3=LETTERS[1:5], value=rnorm(5,10,10)) df %>% rowwise %>% mutate(key=paste(sort(c(grp1, grp2)),…
jO.
  • 2,834
  • 7
  • 25
  • 33
3
votes
2 answers

Row operations on selected columns based on substring in data.table

I would like to apply a function to selected columns that match two different substrings. I've found this post related to my question but I couldn't get an answer from there. Here is a reproducible example with my failed attempt. For the sake of…
rafa.pereira
  • 10,729
  • 4
  • 59
  • 88
3
votes
2 answers

Pandas dataframe finding largest N elements of each row with row-specific N

I have a DataFrame: >>> df = pd.DataFrame({'row1' : [1,2,np.nan,4,5], 'row2' : [11,12,13,14,np.nan], 'row3':[22,22,23,24,25]}, index = 'a b c d e'.split()).T >>> df a b c d e row1 1.0 2.0 NaN 4.0 5.0 row2 11.0 …
Zhang18
  • 4,031
  • 8
  • 43
  • 61
1
2 3
8 9