tidyr

Reshape multiple values at once

牧云@^-^@ 提交于 2019-11-26 02:09:49
问题 I have a long data set I would like to make wide and I\'m curious if there is a way to do this all in one step using the reshape2 or tidyr packages in R. The data frame df looks like this: id type transactions amount 20 income 20 100 20 expense 25 95 30 income 50 300 30 expense 45 250 I\'d like to get to this: id income_transactions expense_transactions income_amount expense_amount 20 20 25 100 95 30 50 45 300 250 I know I can get part of the way there with reshape2 via for example: dcast(df,

R spreading multiple columns with tidyr [duplicate]

﹥>﹥吖頭↗ 提交于 2019-11-26 01:45:03
问题 This question already has an answer here: How can I spread repeated measures of multiple variables into wide format? 4 answers Take this sample variable df <- data.frame(month=rep(1:3,2), student=rep(c(\"Amy\", \"Bob\"), each=3), A=c(9, 7, 6, 8, 6, 9), B=c(6, 7, 8, 5, 6, 7)) I can use spread from tidyr to change this to wide format. > df[, -4] %>% spread(student, A) month Amy Bob 1 1 9 8 2 2 7 6 3 3 6 9 But how can I spread two values e.g. both A and B , such that the output is something like

How can I spread repeated measures of multiple variables into wide format?

守給你的承諾、 提交于 2019-11-26 01:36:10
问题 I\'m trying to take columns that are in long format and spread them to wide format as shown below. I\'d like to use tidyr to solve this with the data manipulation tools I\'m investing in but to make this answer more general please provide other solutions. Here\'s what I have: library(dplyr); library(tidyr) set.seed(10) dat <- data_frame( Person = rep(c(\"greg\", \"sally\", \"sue\"), each=2), Time = rep(c(\"Pre\", \"Post\"), 3), Score1 = round(rnorm(6, mean = 80, sd=4), 0), Score2 = round

Combine Multiple Columns Into Tidy Data [duplicate]

和自甴很熟 提交于 2019-11-26 00:34:22
问题 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 . My dataset looks like this: unique.id abx.1 start.1 stop.1 abx.2 start.2 stop.2 abx.3 start.3 stop.3 abx.4 start.4 1 1 Moxi 2014-01-01 2014-01-07 PenG 2014-01-01 2014-01-07 Vanco 2014-01-01 2014-01-07 Moxi 2014-01-01 2 2 Moxi 2014-01-01 2014-01-02 Cipro 2014-01-01 2014-01-02 PenG 2014-01-01 2014-01-02 Vanco 2014-01-01 3 3

Gather multiple sets of columns [duplicate]

这一生的挚爱 提交于 2019-11-25 23:01:37
问题 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 have data from an online survey where respondents go through a loop of questions 1-3 times. The survey software (Qualtrics) records this data in multiple columns—that is, Q3.2 in the survey will have columns Q3.2.1. , Q3.2.2. , and Q3.2.3. : df <- data.frame( id = 1:10, time = as.Date(\'2009-01-01\') + 0:9, Q3.2.1. = rnorm

Reshaping multiple sets of measurement columns (wide format) into single columns (long format)

♀尐吖头ヾ 提交于 2019-11-25 22:12:50
问题 I have a dataframe in a wide format, with repeated measurements taken within different date ranges. In my example there are three different periods, all with their corresponding values. E.g. the first measurement ( Value1 ) was measured in the period from DateRange1Start to DateRange1End : ID DateRange1Start DateRange1End Value1 DateRange2Start DateRange2End Value2 DateRange3Start DateRange3End Value3 1 1/1/90 3/1/90 4.4 4/5/91 6/7/91 6.2 5/5/95 6/6/96 3.3 I\'m looking to reshape the data to

R spreading multiple columns with tidyr [duplicate]

二次信任 提交于 2019-11-25 19:08:40
This question already has an answer here: How can I spread repeated measures of multiple variables into wide format? 4 answers Take this sample variable df <- data.frame(month=rep(1:3,2), student=rep(c("Amy", "Bob"), each=3), A=c(9, 7, 6, 8, 6, 9), B=c(6, 7, 8, 5, 6, 7)) I can use spread from tidyr to change this to wide format. > df[, -4] %>% spread(student, A) month Amy Bob 1 1 9 8 2 2 7 6 3 3 6 9 But how can I spread two values e.g. both A and B , such that the output is something like month Amy.A Bob.A Amy.B Bob.B 1 1 9 8 6 5 2 2 7 6 7 6 3 3 6 9 8 7 Here's a possible both simple and very