reshape2

Is it possible to use spread on multiple columns in tidyr similar to dcast?

久未见 提交于 2019-11-26 12:21:47
问题 I have the following dummy data: library(dplyr) library(tidyr) library(reshape2) dt <- expand.grid(Year = 1990:2014, Product=LETTERS[1:8], Country = paste0(LETTERS, \"I\")) %>% select(Product, Country, Year) dt$value <- rnorm(nrow(dt)) I pick two product-country combinations sdt <- dt %>% filter((Product == \"A\" & Country == \"AI\") | (Product == \"B\" & Country ==\"EI\")) and I want to see the values side by side for each combination. I can do this with dcast : sdt %>% dcast(Year ~ Product

Reshaping wide to long with multiple values columns [duplicate]

Deadly 提交于 2019-11-26 11:45:52
This question already has an answer here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) 7 answers I need to reshape my wide table into long format but keeping multiple fields for each record, for example: dw <- read.table(header=T, text=' sbj f1.avg f1.sd f2.avg f2.sd blabla A 10 6 50 10 bA B 12 5 70 11 bB C 20 7 20 8 bC D 22 8 22 9 bD ') # Now I want to melt this table, keeping both AVG and SD as separate fields for each measurement, to get something like this: # sbj var avg sd blabla # A f1 10 6 bA # A f2 50 10 bA # B f1 12 5 bB # B f2 70 11

how to spread or cast multiple values in r [duplicate]

只谈情不闲聊 提交于 2019-11-26 11:28:23
问题 This question already has an answer here: can the value.var in dcast be a list or have multiple value variables? 3 answers Convert data from long format to wide format with multiple measure columns 5 answers Here is toy data set for this example: data <- data.frame(x=rep(c(\"red\",\"blue\",\"green\"),each=4), y=rep(letters[1:4],3), value.1 = 1:12, value.2 = 13:24) x y value.1 value.2 1 red a 1 13 2 red b 2 14 3 red c 3 15 4 red d 4 16 5 blue a 5 17 6 blue b 6 18 7 blue c 7 19 8 blue d 8 20 9

Reshape multiple values at once

女生的网名这么多〃 提交于 2019-11-26 10:31:56
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, id ~ type, value.var="transactions") But is there a way to reshape the entire df in one shot addressing

Convert a dataframe to presence absence matrix

淺唱寂寞╮ 提交于 2019-11-26 08:37:45
问题 I have a table which has unequal number of element in string format File1 A B C File2 A B D File3 E F I want to convert into a format as follows A B C D E F File1 1 1 1 0 0 0 FIle2 1 1 0 1 0 0 File3 0 0 0 0 1 1 I tried to do it using reshape2 but was not successful. Sample data: mydata <- structure(list(V1 = c(\"File1\", \"File2\", \"File3\"), V2 = c(\"A\", \"A\", \"E\"), V3 = c(\"B\", \"B\", \"F\"), V4 = c(\"C\", \"D\", \"\")), .Names = c(\"V1\", \"V2\", \"V3\", \"V4\"), class = \"data.frame

Grouped bar plot in ggplot

匆匆过客 提交于 2019-11-26 06:05:50
问题 I have a survey file in which row are observation and column question. Here are some fake data they look like: People,Food,Music,People P1,Very Bad,Bad,Good P2,Good,Good,Very Bad P3,Good,Bad,Good P4,Good,Very Bad,Very Good P5,Bad,Good,Very Good P6,Bad,Good,Very Good My aim is to create this kind of plot with ggplot2 . I absolutely don\'t care of the colors, design, etc. The plot doesn\'t correspond to the fake data Here are my fake data: raw <- read.csv(\"http://pastebin.com/raw.php?i

melt / reshape in excel using VBA?

两盒软妹~` 提交于 2019-11-26 03:56:42
问题 I\'m currently adjusting to a new job where most of the work I share with colleagues is via MS Excel. I am using pivot tables frequently, and therefore need \"stacked\" data, precisely the output of the melt() function in the reshape (reshape2) package in R that I\'ve come to rely on for this. Could anyone get me started on a VBA macro to accomplish this, or does one exist already? The outline of the macro would be: Select a range of cells in an Excel workbook. Start \"melt\" macro. Macro

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,

Reshaping wide to long with multiple values columns [duplicate]

不问归期 提交于 2019-11-26 01:16:06
问题 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 need to reshape my wide table into long format but keeping multiple fields for each record, for example: dw <- read.table(header=T, text=\' sbj f1.avg f1.sd f2.avg f2.sd blabla A 10 6 50 10 bA B 12 5 70 11 bB C 20 7 20 8 bC D 22 8 22 9 bD \') # Now I want to melt this table, keeping both AVG and SD as separate fields for

Faster ways to calculate frequencies and cast from long to wide

时间秒杀一切 提交于 2019-11-25 22:15:47
问题 I am trying to obtain counts of each combination of levels of two variables, \"week\" and \"id\". I\'d like the result to have \"id\" as rows, and \"week\" as columns, and the counts as the values. Example of what I\'ve tried so far (tried a bunch of other things, including adding a dummy variable = 1 and then fun.aggregate = sum over that): library(plyr) ddply(data, .(id), dcast, id ~ week, value_var = \"id\", fun.aggregate = length, fill = 0, .parallel = TRUE) However, I must be doing