tidyr

tidyr::gather vs. reshape2::melt on matrices

房东的猫 提交于 2019-11-30 23:45:06
问题 I've been a long time user of reshape2::melt in a rather non-standard way: I'm running numeric experiments and get a matrix as a result. I then melt it and produce some images. Inspired by the similarity between reshape2 and tidyr , I'm now trying to achieve identical output on objects of class matrix. No luck so far: library(reshape2) library(tidyr) set.seed(42) mat <- matrix(runif(6), 3) mat2 <- mat colnames(mat2) <- letters[1:2] rownames(mat2) <- letters[3:5] melt(mat) melt(mat2) gather

using tidyverse; counting after and before change in value, within groups, generating new variables for each unique shift

会有一股神秘感。 提交于 2019-11-30 17:46:13
I am looking for a tidyverse -solution that can count occurrences of unique values of TF within groups, id in the data data tbl . When TF changes I want to count both forward and backwards from that point. This counting should be stored in a new variable PM## , so that PM## holds both plus and minus to each unique shift in TF . This question is similar to a question I previously asked , but here I am specifically looking for a solution using tidyverse tools. Uwe provided an elegant answer to the inital question using data.table here . If this question violates any SO policies please let me

tidyr wide to long with two repeated measures

筅森魡賤 提交于 2019-11-30 15:57:16
问题 I have some data that is not tidy. It has two nested repeated measures (Q1/Q2 nested within Constructs). I'd like to move it from wide to long format. ## id time Q1..Ask Q2..Ask Q1..Tell Q2..Tell Q1..Respond Q2..Respond ## 1 1 pre 1 1 1 1 0 0 ## 2 2 pre 0 1 1 0 0 1 ## 3 3 pre 0 0 1 0 0 0 ## 4 4 pre 1 1 0 1 1 0 ## 5 5 pre 0 0 0 0 0 0 ## 6 1 post 0 0 1 1 0 1 ## 7 2 post 0 0 1 1 0 0 ## 8 3 post 0 0 0 1 0 0 ## 9 4 post 1 0 1 1 0 0 ## 10 5 post 0 1 0 1 1 1 Here question 1 and question 2 (Q1 & Q2)

Dplyr summarise_each to aggregate results

别等时光非礼了梦想. 提交于 2019-11-30 15:47:53
问题 I have a data frame as such: metric1 metric2 metric3 field1 field2 1 1.07809668 4.2569882 7.1710095 L S1 2 0.56174763 1.2660273 -0.3751915 L S2 3 1.17447327 5.5186679 11.6868322 L S2 4 0.32830724 -0.8374830 1.8973718 S S2 5 -0.51213503 -0.3076640 10.0730274 S S1 6 0.24133119 2.7984703 15.9622215 S S1 7 1.96664414 0.1818531 2.7416768 S S3 8 0.06669409 3.8652075 10.5066330 S S3 9 1.14660437 8.5703119 3.4294062 L S4 10 -0.72785683 9.3320762 1.3827989 L S4 I am showing 2 fields but have several

Dplyr summarise_each to aggregate results

柔情痞子 提交于 2019-11-30 15:10:14
I have a data frame as such: metric1 metric2 metric3 field1 field2 1 1.07809668 4.2569882 7.1710095 L S1 2 0.56174763 1.2660273 -0.3751915 L S2 3 1.17447327 5.5186679 11.6868322 L S2 4 0.32830724 -0.8374830 1.8973718 S S2 5 -0.51213503 -0.3076640 10.0730274 S S1 6 0.24133119 2.7984703 15.9622215 S S1 7 1.96664414 0.1818531 2.7416768 S S3 8 0.06669409 3.8652075 10.5066330 S S3 9 1.14660437 8.5703119 3.4294062 L S4 10 -0.72785683 9.3320762 1.3827989 L S4 I am showing 2 fields but have several more. I need to sum the metrics grouped by each field e.g. for field1: DF %>% group_by(field1) %>%

using tidyr unnest with NULL values

混江龙づ霸主 提交于 2019-11-30 13:54:09
I converted a JSON file into a data.frame with a a nested list structure, which I would like to unnest and flatten. Some of the values in the list are NULL, which unnest does not accept. If I replace the NULL values with a data.frame structure that has only NA values, I get the desired result. Below is a simplified example of my problem. I have tried to replace the NULL values with the NA data.frame but did not manage because of the the nested structure. How can I achieve the desired result? Example library(tidyr) input1 <- data.frame(id = c("c", "d", "e"), value = c(7, 8, 9)) input2 <- NULL

group_by() into fill() not working as expected

风流意气都作罢 提交于 2019-11-30 12:38:46
I'm trying to do a Last Observation Carried Forward operation on some poorly formatted data using dplyr and tidyr . It isn't working as I'd expect. library(dplyr) library(tidyr) df <- data.frame(id=c(1,1,2,2,3,3), email=c('bob@email.com', NA, 'joe@email.com', NA, NA, NA)) df2 <- df %>% group_by(id) %>% fill(email) This results in: Source: local data frame [6 x 2] Groups: id [3] id email (dbl) (fctr) 1 1 bob@email.com 2 1 bob@email.com 3 2 joe@email.com 4 2 joe@email.com 5 3 joe@email.com 6 3 joe@email.com I expect it to be: Source: local data frame [6 x 2] Groups: id [3] id email (dbl) (fctr)

Using gather() to gather two (or more) groups of columns into two (or more) key-value pairs [duplicate]

被刻印的时光 ゝ 提交于 2019-11-30 09:32:00
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed 2 years ago . I want to gather two seperate groups of columns into two key-value pairs. Here's some example data: library(dplyr) library(tidyr) ID = c(1:5) measure1 = c(1:5) measure2 = c(6:10) letter1 = c("a", "b", "c", "d", "e") letter2 = c("f", "g", "h", "i", "j") df = data.frame(ID, measure1, measure2, letter1, letter2) df = tbl_df(df)

tidyr: multiple unnesting with varying NA counts

不羁的心 提交于 2019-11-30 08:46:48
问题 I'm confused about some tidyr behavior. I can unnest a single response like this: library(tidyr) resp1 <- c("A", "B; A", "B", NA, "B") resp2 <- c("C; D; F", NA, "C; F", "D", "E") resp3 <- c(NA, NA, "G; H; I", "H; I", "I") data <- data.frame(resp1, resp2, resp3, stringsAsFactors = F) tidy <- data %>% transform(resp1 = strsplit(resp1, "; ")) %>% unnest() # Source: local data frame [6 x 3] # # resp2 resp3 resp1 # (chr) (chr) (chr) # 1 C; D; F NA A # 2 NA NA B # 3 NA NA A # 4 C; F G; H; I B # 5 D

using tidyverse; counting after and before change in value, within groups, generating new variables for each unique shift

折月煮酒 提交于 2019-11-30 01:17:47
问题 I am looking for a tidyverse-solution that can count occurrences of unique values of TF within groups, id in the data data tbl . When TF changes I want to count both forward and backwards from that point. This counting should be stored in a new variable PM## , so that PM## holds both plus and minus to each unique shift in TF . This question is similar to a question I previously asked, but here I am specifically looking for a solution using tidyverse tools. Uwe provided an elegant answer to