reshape2

how to pivot/unpivot (cast/melt) data frame? [duplicate]

北城余情 提交于 2019-11-27 04:37:17
This question already has an answer here: Reshaping data.frame from wide to long format 6 answers How can I 'unpivot' a table? What is the proper technical term for this? UPDATE: The term is called melt I have a data frame for countries and data for each year Country 2001 2002 2003 Nigeria 1 2 3 UK 2 NA 1 And I want to have something like Country Year Value Nigeria 2001 1 Nigeria 2002 2 Nigeria 2003 3 UK 2001 2 UK 2002 NA UK 2003 1 I still can't believe I beat Andrie with an answer. :) > library(reshape) > my.df <- read.table(text = "Country 2001 2002 2003 + Nigeria 1 2 3 + UK 2 NA 1", header

Convert a dataframe to presence absence matrix

妖精的绣舞 提交于 2019-11-27 02:24:48
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", row.names = c(NA, -3L)) One possibility: library(reshape2) df2 <- melt(df, id.var = "V1") with(df2, table(V1, value)) # value # V1 A

Can dcast be used without an aggregate function? [duplicate]

本秂侑毒 提交于 2019-11-27 01:20:56
Possible Duplicate: This R reshaping should be simple, but dcast from reshape2 works without a formula where there are no duplicates. Take these example data: df <- structure(list(id = c("A", "B", "C", "A", "B", "C"), cat = c("SS", "SS", "SS", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 224L, 225L, 2206L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, -6L)) I'd like to dcast these data and just have the values tabulated, without applying any function to the value.var including the default length . In this case, it works fine. > dcast(df, id~cat, value.var="val") id

Tidy data.frame with repeated column names

倾然丶 夕夏残阳落幕 提交于 2019-11-26 22:01:22
问题 I have a program that gives me data in this format toy file_path Condition Trial.Num A B C ID A B C ID A B C ID 1 root/some.extension Baseline 1 2 3 5 car 2 1 7 bike 4 9 0 plane 2 root/thing.extension Baseline 2 3 6 45 car 5 4 4 bike 9 5 4 plane 3 root/else.extension Baseline 3 4 4 6 car 7 5 4 bike 68 7 56 plane 4 root/uniquely.extension Treatment 1 5 3 7 car 1 7 37 bike 9 8 7 plane 5 root/defined.extension Treatment 2 6 7 3 car 4 6 8 bike 9 0 8 plane My goal is to tidy the format into

R reshape a vector into multiple columns

安稳与你 提交于 2019-11-26 20:53:53
问题 Let's say I have a vector in R as follows: d<-seq(1,100) I want to reshape this vector into a 10x10 matrix, so that I will have this data instead: [,1] [,2] [,3] .. [,10] 1 2 3 .. 10 11 12 13 .. 20 21 22 23 .. 30 .. 91 92 93 .. 100 I tried to use reshape function, but it didn't work. Can someone help please? 回答1: You can do dim(d) <- c(10, 10) d <- t(d) or d <- matrix(d, nrow = 10, byrow = TRUE) 回答2: If you want to convert a predifined list to a matrix (e.g. a 5*4 matrix), do yourMatrix <-

Transposing data frames

痴心易碎 提交于 2019-11-26 20:42:42
问题 Happy Weekends. I've been trying to replicate the results from this blog post in R. I am looking for a method of transposing the data without using t , preferably using tidyr or reshape . In example below, metadata is obtained by transposing data . metadata <- data.frame(colnames(data), t(data[1:4, ]) ) colnames(metadata) <- t(metadata[1,]) metadata <- metadata[-1,] metadata$Multiplier <- as.numeric(metadata$Multiplier) Though it achieves what I want, I find it little unskillful. Is there any

reshape vs. reshape2 in R

社会主义新天地 提交于 2019-11-26 19:47:58
问题 I am attempting to understand why development had shifted from reshape to reshape2 package. They seem to be functionally the same, however, I am unable to upgrade to reshape2 currently due to an older version of R running on the server. I am concerned about the possibility of a major bug that would have shifted development to a whole new package instead of simply continuing development of reshape . Does anyone know if there is a major flaw in the reshape package? 回答1: reshape2 let Hadley make

Reshape a dataframe to long format with multiple sets of measure columns [duplicate]

帅比萌擦擦* 提交于 2019-11-26 17:51:58
问题 This question already has an answer here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) 7 answers I have an R dataframe that I scraped from the internet using readHTMLTable() in the XML package. The table looks like the following excerpt with multiple variables/columns for population and year. (Note that the years are not duplicated across columns and represent a unique identifier for population.) year1 pop1 year2 pop2 year3 pop3 1 2 16XX 4675

Grouped bar plot in ggplot

你说的曾经没有我的故事 提交于 2019-11-26 17:12:25
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=L8cEKcxS",sep=",") raw[,2]<-factor(raw[,2],levels=c("Very Bad","Bad","Good","Very Good"),ordered=FALSE) raw[,3]<

Reshaping data frame with duplicates

允我心安 提交于 2019-11-26 14:30:23
问题 I have what should be a simple reshaping problem, but I can't figure it out. Part of my data looks like this: foo <- structure(list(grade = c(3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 3, 3, 4, 4, 5, 5, 6, 6), var.type = structure(c(3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L ), .Label = c("Raw Score", "SE", "SS"), class = "factor"), var.val = c(120L, 47L, 120L, 46L, 120L, 46L, 120L, 47L, 120L, 46L, 120L, 46L, 120L, 12L, 120L, 14L, 120L, 16L, 120L, 20L)), .Names =