Questions tagged [rowwise]

229 questions
1
vote
1 answer

Rowwise operation within for loop using dplyr

I have some transport data which I would like to perform a rowwise if comparison within a for loop. The data looks something like this. # Using the iris dataset > iris <- as.data.frame(iris) > head(iris) Sepal.Length Sepal.Width Petal.Length…
HiChiu5493
  • 25
  • 5
1
vote
1 answer

Store setdiff results in dataframe cell

Here is my example: library(dplyr) my_df <- data.frame( col_1 = I(list(c(1,2), 1 )), col_2 = c(1,2)) my_df <- my_df %>% rowwise(diff = setdiff(col_1, col_2)) It generates an error: Error in rowwise(., diff = setdiff(col_1, col_2)) : unused…
user1700890
  • 7,144
  • 18
  • 87
  • 183
1
vote
1 answer

sample with dplyr and rowwise

Here is my example: library(dplyr) n_experiments <- 1000 a <- sample(1:3, n_experiments, replace = T) b <- sample(1:3, n_experiments, replace = T) my_df <- data.frame(a = a, b= b) set.seed(7);my_df <- my_df %>% rowwise() %>% mutate(col_1 =…
user1700890
  • 7,144
  • 18
  • 87
  • 183
1
vote
1 answer

Row-wise compare two arrays of unequal sizes but same dimensions. (Python)

I have two arrays A and B of sizes (m,2) and (n,2). A and B store the indices of non-zero elements in two different matrices P and Q. I want to find the indices which match, so I can find which indices store non-zero values.
Kimi
  • 11
  • 1
1
vote
2 answers

Get row wise sum considering all the previous sums in SQL Server

Tried to get row wise sum considering all the previous sums and got output shown below. Can please anybody tell me what i'm missing? SELECT ProductId, ProductName, ProductAmount As ProductActualAmount, CASE WHEN (CASE WHEN…
Santosh Jadi
  • 1,479
  • 6
  • 29
  • 55
1
vote
2 answers

difference between first non-NA and last non-NA in each row

I have a data frame with up to 5 measurements (x) and their corresponding time: df = structure(list(x1 = c(92.9595722286402, 54.2085219673818, 46.3227062573019, NA, 65.1501442134141, 49.736451235317), time1 = c(43.2715277777778, 336.625,…
Omry Atia
  • 2,411
  • 2
  • 14
  • 27
1
vote
1 answer

Element-wise division by rows between dataframe and series

I've just started with pandas some weeks ago and now I am trying to perform an element-wise division on rows, but couldn't figure out the proper way to achieve it. Here is my case and data date type id ... 1096 …
gustavovelascoh
  • 1,208
  • 1
  • 14
  • 28
1
vote
1 answer

Applying function row-wise in a data.table; passing column names as a vector

Consider a function foo as follows. foo <- function(a, b, c) { out <- (sum(a) + sqrt(prod(c))) / sqrt(pi * b) return(out) } I would like to apply the function to a data.table DT with the data in columns as arguments, row-wise according to a…
Crops
  • 5,024
  • 5
  • 38
  • 65
1
vote
1 answer

R dplyr: rowwise + mutate (+glue) - how to get/refer row content?

The simple example of input data: dataset <- data.frame("part1" = c("a", "b", "c"), "part2" = c("x", "y", "z"), "caption" = c("{part1} {part2}", "{part2} {part1}", …
RSzT
  • 307
  • 3
  • 14
1
vote
3 answers

How to calculate the sequence of start and end dates efficiently in a tibble?

I have the following starting point: #dataset: schedule <- tibble(start = as.Date(c("2018-07-11", NA, NA)), duration = c(10,23,9),flag_StartActual = c(TRUE,FALSE,FALSE)) in table format: > schedule # A tibble: 3 x 3 start duration…
Jochem
  • 3,295
  • 4
  • 30
  • 55
1
vote
1 answer

R dplyr rowwise mutate

Good morning all, this is my first time posting on stack overflow. Thank you for any help! I have 2 dataframes that I am using to analyze stock data. One data frame has dates among other information, we can call it df: df1 <- tibble(Key =…
Jesse Kerr
  • 341
  • 1
  • 8
1
vote
1 answer

How to dynamically conditionally filter rows in R dataframe

Suppose I have an ordered data frame that looks like this: df <- data.frame(customer = c('cust1','cust1','cust2','cust3','cust3'), start_month = as.Date(c('2016-03-01','2017-08-01','2016-03-01','2017-07-01','2017-10-01')), price =…
Saul Feliz
  • 668
  • 3
  • 11
  • 20
1
vote
1 answer

How to randomly select one nonzero element per row from a sparse matrix with out for loop in python

I have a large sparse matrix whose each row contains multiple nonzero elements, for example a = np.array([[1, 1,0,0,0,0], [2,0, 1,0,2,0], [3,0,4,0,0, 3]]) I want to be able to randomly select one nonzero element per row without for loop. Any good…
amina mollaysa
  • 387
  • 2
  • 4
  • 18
1
vote
1 answer

dplyr rowwise and mutate with custom function returns unexpected output

I have a dataframe that looks like this in R: library(dplyr) group <- c(1,2,3,4,5,6) num_click <- c(33000, 34000, 35000, 33500, 34500, 32900) num_open <- c(999000, 999500, 1000000, 1000050, 985000, 999999) df <- data.frame(group, num_click,…
ian
  • 43
  • 3
1
vote
0 answers

Has the use of rowwise() changed in dplyr 0.7?

I have until now been using a variation of an example of rowwise() provided in a previous question successfully but with the dplyr 0.7 update I am getting different output shown below: df <- data.frame(A=rnorm(10), B=rnorm(10), C=rnorm(10)) …
MrGraeme
  • 97
  • 1
  • 1
  • 8