Questions tagged [rowwise]

229 questions
5
votes
1 answer

openxlsx::writeFormula rowwise - is there an efficient way to do this?

openxlsx functions to write formula seem to add formula columnwise only. Is there an efficient way to write formula rowwise? Working through a reproducible example to illustrate the issue: df <- data.frame(a = 1:3, b = 4:6, …
Peter
  • 11,500
  • 5
  • 21
  • 31
5
votes
6 answers

Sum columns row-wise with similar names

I have a dataframe that has lots of columns that are something like this: data <- data.frame (a.1 = 1:5, a.2b = 3:7, a.5 = 5:9, bt.16 = 4:8, bt.12342 = 7:11) I'd like a result with columns that sum the variables that have the same prefix. In this…
TSim
  • 71
  • 2
  • 5
4
votes
1 answer

dplyr: Why do some operations work "rowwise" without calling rowwise() and others dont?

I am still trying to figure out, how rowwise works exactly in R/dplyr. For example I have this code: library(dplyr) df = data.frame( group = c("a", "a", "a", "b", "b", "c"), var1 = 1:6, var2 = 7:12 ) df %>% mutate( concatNotRW =…
Lenn
  • 1,283
  • 7
  • 20
4
votes
4 answers

How to fill NA's when you have enough information to calculate what the NA should be in R

I've got a dataset where there are some NA's but I can manually work out what the values should be as the df is a column for name and the rest of the columns are just numbers followed by a final column with total. Only one NA appears per row at most…
P_S_13
  • 65
  • 3
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
  • 72,363
  • 6
  • 19
  • 66
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
  • 17,549
  • 4
  • 18
  • 39
4
votes
2 answers

How to summarise across different types of variables with dplyr::c_across()

I have data with different types of variables. Some are character, some factors, and some numeric, like below: df <- data.frame(a = c("tt", "ss", "ss", NA), b=c(2,3,NA,1), c=c(1,2,NA, NA), d=c("tt", "ss", "ss", NA)) I'm trying to count the number…
msoftrain
  • 1,017
  • 8
  • 23
4
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. #…
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
  • 15,909
  • 12
  • 89
  • 97
4
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,800
  • 10
  • 50
  • 67
3
votes
4 answers

Efficient way to implement rule-based value assignment

I'm trying to come up with an elegant, rule-based way to assign codes to rows in a data frame based on combinations of values in columns, using this data: library(tidyr) df <- crossing(yr2018=c("M","S","W"), yr2019=c("M","S","W"), …
E Maas
  • 115
  • 7
3
votes
6 answers

Applying function rowwise efficiently

I have a dataframe with multiple columns containing information on one diagnosis. The entries are TRUE, FALSE or NA. I create a vector which summarizes those columns as follows: If a patient was diagnosed at some time (TRUE), then TRUE, if the only…
LulY
  • 976
  • 1
  • 9
  • 24
3
votes
2 answers

replace for loop with dplyr across / rowwise?

I'm having a hard time refactoring a for loop into a dplyr pipe. I need to reference the dataframa a and the previously calculated row. Any advice how to get b from a on a dplyr pipe? Many thanks! a <- tibble::tribble(~ 'a', ~ 'b', ~ 'c', …
cg1979
  • 100
  • 5
3
votes
2 answers

R Avoid rowwise() and looking for faster alternative

I want to merge two vectors into one dataset and integrate it with the function mutate as 5 new columns into the already existing dataset. Here is my example…
TobKel
  • 1,293
  • 8
  • 20
3
votes
3 answers
1
2
3
15 16