Questions tagged [rowwise]

229 questions
3
votes
1 answer

Problem with row-wise operation in base R

I have a problem with performing row-wise operations using 'apply' function in R. I want to calculate the distance between two points: d <- function(x,y){ length <- norm(x-y,type="2") as.numeric(length) } The coordinates are given by two…
Rafau
  • 47
  • 4
3
votes
4 answers

R+dplyr: conditionally swap the elements of two columns

Consider the dataframe df at the end of the post. I simply would like to swap the elements of columns x and y whenever x>y. There may be other columns in the dataframe which I do not want to touch. In a sense, I would like to sort row wise the…
larry77
  • 1,309
  • 14
  • 29
3
votes
3 answers

Compare character rows of a df matching NA to everything and create new column or df based on comparison

I have a very large dataframe with character values. I want to compare the rows to each other and create IDs based on the comparison. The problem is that there are NA in the df and that I want those be evaluated as matching any value. The other…
SandraA.
  • 95
  • 1
  • 10
3
votes
3 answers

R dplyr replace "missing" column data with first non-"missing" value

This is a tricky one to describe concisely in a headline (or to google). I have a taxonomy table where some columns may be listed as "dropped' based on a confidence level. I'd like to replace any column that says "dropped" with "Unidentified"…
Luther Blissett
  • 373
  • 1
  • 10
3
votes
2 answers

R: Calculate rowwise lm() over multiple variables

How can I calculate the row-wise lm() / coeffs of multiple variables in an df, which are stored in columns? I have this kind of data (just examples): set.seed(1) foo <- data.frame(trialNumber= 1:10, Nr1 = runif(10), Nr2 = runif(10), Nr3 =…
basti41a
  • 153
  • 1
  • 6
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
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.
  • 3,384
  • 7
  • 28
  • 38
3
votes
3 answers

dplyr - apply a custom function using rowwise()

I have a data frame and want to count the number of zeros in each row using dplyr's rowwise. What am I doing wrong? dt2 = data.frame(A = c(8, 6), B = c(0, 0), C = c(0, 5)) dt2 zerocount <- function(x) {sum(x == 0)} library(dplyr) dt2 %>% rowwise()…
user3245256
  • 1,842
  • 4
  • 24
  • 51
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
  • 13,251
  • 6
  • 71
  • 109
3
votes
2 answers

How to do row wise operations on .SD columns in data.table

Although I've figured this out before, I still find myself searching (and unable to find) this syntax on stackoverflow, so... I want to do row wise operations on a subset of the data.table's columns, using .SD and .SDcols. I can never remember if…
geneorama
  • 3,620
  • 4
  • 30
  • 41
2
votes
2 answers

How to impute a conditional row-wise imputation of a constant

I am somewhat of an R newbie, am struggling with writing code for what seems like simple logic, and would appreciate any help! I am trying to impute a constant value of 1 for NA cells in each row of my data set but only for rows that have 2 or less…
kaho
  • 23
  • 3
2
votes
2 answers

How to sum values in a row wise fashion and always selecting all columns apart from first in R

is there a way to select number of columns without explicitly typing the column names. This is more for automation purposes. Consider the following code a <- c("people", "contracts", "design") b <- as.numeric(c(1,2,3)) c <- as.numeric(c(2,3,4)) d <-…
TheBoomerang
  • 109
  • 5
2
votes
5 answers

Cumulative sum for specific range of dates

I'm trying to calculate the rowise cumulative sum of Rates from DATE to DATE_following. For example: library(tidyverse) library(bizdays) library(lubridate) set.seed(1) dat <- seq.Date(from = as.Date(as.Date("2023-04-06")- days(10)), …
2
votes
1 answer

How to count occurrences of a specific value across multiple columns?

I want to count how many times a specific value occurs across multiple columns and put the number of occurrences in a new column. My dataset has a lot of missing values but only if the entire row consists solely of NA's, it should return NA. If…
2
votes
2 answers

group_by() and summarise() by row

I have a data with several line ids per time and with -infinite values, and I would like to use the R packages dplyr and tidyverse to calculate the average number of -infinite per ID per time. This is my data: dt <- data.frame(id = rep(1:3, each =…
Ph.D.Student
  • 704
  • 6
  • 27
1 2
3
15 16