rowwise

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

跟風遠走 提交于 2020-12-31 06:46:11
问题 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"), c("8","12"),c("8","12")) ascending <- c("+","-","+","-") position <- c(11,6,9,8) desired_output <- c(5,5,"disqualified",3) bridge_game <- data.frame(start = I(start), end = I(end), ascending = ascending, position = position, desired_output = desired_output)

dplyr mutate - How do I pass one row as a function argument?

亡梦爱人 提交于 2020-06-26 14:12:13
问题 I'm trying to create a new column in my tibble which collects and formats all words found in all other columns. I would like to do this using dplyr, if possible. Original DataFrame: df <- read.table(text = " columnA columnB 1 A Z 2 B Y 3 C X 4 D W 5 E V 6 F U " ) As a simplified example, I am hoping to do something like: df %>% rowwise() %>% mutate(newColumn = myFunc(.)) And have the output look like this: columnA columnB newColumn 1 A Z AZ 2 B Y BY 3 C X CX 4 D W DW 5 E V EV 6 F U FU When I

Rowwise operation with adaptive range using dplyr

老子叫甜甜 提交于 2020-05-28 04:53:36
问题 Based on my earlier question, I would like to calculate colocation (i.e. two people appearing at the same time) instances given a smartcard data. Here is a made-up sample consisting of ten records: library(lubridate) smartcard <- c(1,2,3,2,1,2,4,4,1,1) boarding_stop <- c("C23", "C14", "C23", "C23", "C23", "C14", "C14", "C23", "C14", "C23") boarding_time <- as.times(c("07:24:01", "07:26:18", "07:37:19", "08:29:22", "08:34:10", "15:55:23", "16:20:22", "17:07:31", "17:13:34", "17:35:52"))

Rowwise operation within for loop using dplyr

偶尔善良 提交于 2020-05-12 07:58:16
问题 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 Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa Where the result would record the instances of sepal lengths with equal petal width

Rowwise operation within for loop using dplyr

久未见 提交于 2020-05-12 07:57:49
问题 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 Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa Where the result would record the instances of sepal lengths with equal petal width

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

别等时光非礼了梦想. 提交于 2020-01-25 00:38:08
问题 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}", "{part2} {part1} {part2}"), stringsAsFactors = F) Expected results: # A tibble: 3 x 3 part1 part2 caption <chr> <chr> <chr> 1 a x a x 2 b y y b 3 c z z c z The below code doesn't work, because . refers to the whole dataset , instead of data of the whole row content: dataset %>% rowwise() %>% mutate("caption" = glue::glue_data(.,

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

前提是你 提交于 2019-12-24 11:28:57
问题 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 unique key column ID . DT <- structure(list(ID = c("K1L1", "K1L2", "K1L3", "K2L1", "K2L2", "K2L3", "K3L1", "K3L2", "K3L3", "K4L1", "K4L2", "K4L3", "K5L1", "K5L2", "K5L3"), K1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), K2 = c(0L, 0L,

Return list using mutate and rowwise

橙三吉。 提交于 2019-12-22 18:42:39
问题 I'm trying to return a list using mutate and rowwise but get the error shown in the code. These questions Q1 Q2 helped, but I'd like to keep it simple by iterating over rows using rowwise() , and the questions are 3yr 7mth old. Thanks. library(tidyverse) df <- data.frame(Name=c("a","a","b","b","c"),X=c(1,2,3,4,5), Y=c(2,3,4,2,2)) TestFn <- function(X,Y){ Z <- list(X*5,Y/2,X+Y,X*2+5*Y) return (Z) } #this works SingleResult <- TestFn(5,20) #error - Error in mutate_impl(.data, dots) :

dplyr rowwise sum and other functions like max

扶醉桌前 提交于 2019-12-19 09:08:40
问题 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 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa > select(iris, starts_with('Petal')) %>% rowSums() [1] 1.6 1.6 1.5 1.7 1.6 2.1 1.7 1.7 1.6 1.6 1.7 1.8 1.5 1.2 1.4 1.9 1.7 1.7 2.0 1.8 1.9 1.9 1.2 2.2 2.1 1.8 2.0 1.7 1.6 1.8 1.8 1.9 1.6 1.6 1.7

Element-wise division by rows between dataframe and series

无人久伴 提交于 2019-12-11 07:37:43
问题 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 1097 1098 0 2014-06-13 cal 1 ... 17.949524 16.247619 15.465079 1 2014-06-13 cow 32 ... 0.523429 -0.854286 -1.520952 2 2014-06-13 cow 47 ... 7.676000 6.521714 5.892381 3 2014-06-13 cow 107 ... 4.161714 3.048571 2.419048 4 2014-06-13 cow 137 ... 3.781143 2.557143 1.931429 5 2014-06-13