reshape2

Seasonal aggregate of monthly data

痞子三分冷 提交于 2019-12-06 11:50:47
I have dataframe df with x,y,and monthly.year data for each x,y point. I am trying to get the seasonal aggregate. I need to calculate seasonal means i.e. For winter mean of (December,January,February); for Spring mean of (March,April,May), for Summer mean of (June,July,August) and for autumn mean of (September,October,November). The data looks similar to: set.seed(1) df <- data.frame(x=1:3,y=1:3, matrix(rnorm(72),nrow=3) ) names(df)[3:26] <- paste(month.abb,rep(2009:2010,each=12),sep=".") x y Jan.2009 Feb.2009 ... Dec.2010 1 1 1 -0.6264538 1.5952808 ... 2.1726117 2 2 2 0.1836433 0.3295078 ...

R: Collapse multiple boolean columns into single attribute column with new rows for each combination [duplicate]

一个人想着一个人 提交于 2019-12-06 03:29:58
This question already has an answer here: Converting multiple boolean columns to single factor column 4 answers Trying to melt or collapse a dataframe with multiple boolean columns into a two column database with an id column and a column for the collapsed values BUT each value results in a new row. Example beginning: A S1 S2 S3 S4 1 ex1 1 0 0 0 2 ex2 0 1 0 0 3 ex3 0 0 1 0 4 ex4 1 1 0 0 5 ex5 0 1 0 1 6 ex6 0 1 0 0 7 ex7 1 1 1 0 8 ex8 0 1 1 0 9 ex9 0 0 1 0 10 ex10 1 0 0 0 Desired output: A Type ex1 S1 ex2 S2 ex3 S3 ex4 S1 ex4 S2 ex5 S2 ex5 S4 ex6 S2 ex7 S1 ex7 S2 ex7 S3 ex8 S2 ex8 S3 ex9 S3

Reshape data frame from wide to long with re-occuring column names in R

♀尐吖头ヾ 提交于 2019-12-05 22:07:24
I'm trying to convert a data frame from wide to long format using the melt formula. The challenge is that I have multiple column names that are labeled the same. When I use the melt function, it drops the values from the repeat column. I have read similar questions and it was advised to use the reshape function, however I was not able to get it work. To reproduce my starting data frame: conversion.id<-c("1", "2", "3") interaction.num<-c("1","1","1") interaction.num2<-c("2","2","2") conversion.id<-as.data.frame(conversion.id) interaction.num<-as.data.frame(interaction.num) interaction.num2<-as

I am unable to download the reshape2 package in R [closed]

此生再无相见时 提交于 2019-12-05 15:45:55
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am getting this response when trying to install the R package: >installed.packages("reshape2") Package LibPath Version Priority Depends Imports LinkingTo Suggests Enhances License License_is_FOSS License_restricts_use OS_type Archs MD5sum NeedsCompilation Built R version 3.2.2 (2015-08-14) Platform: x86_64-w64

POSIXct values become numeric in reshape2 dcast

匆匆过客 提交于 2019-12-05 08:13:34
I'm trying to use dcast from the latest reshape2 package (1.2.1) to denormalize a data frame (or data.table) where the value.var is a POSIXct type, but in the resulting data frame, the date values have lost their POSIXct class and become numeric. Do I really have to as.POSIXct() every generated column if I want the values back as POSIXct's, or am I missing something? x <- c("a","b"); y <- c("c","d"); z <- as.POSIXct(c("2012-01-01 01:01:01","2012-02-02 02:02:02")); d <- data.frame(x, y, z, stringsAsFactors=FALSE); str(d); library(reshape2); e <- dcast(d, formula = x ~ y, value.var = "z"); str(e

In tidyr, what criteria does the function `gather` use to map a dataframe from wide to long?

纵饮孤独 提交于 2019-12-05 00:38:20
问题 I'm trying to figure out the arguments for gather in the tidyr package. I looked at the documentation, and the syntax looks like: gather(data, key, value, ..., na.rm = FALSE, convert = FALSE) There is an example in the help files: stocks <- data.frame( time = as.Date('2009-01-01') + 0:9, X = rnorm(10, 0, 1), Y = rnorm(10, 0, 2), Z = rnorm(10, 0, 4) ) gather(stocks, stock, price, -time) I'm curious about the last line: gather(stocks, stock, price, -time) Here, stocks is clearly the data we

Select a value for based on a highest value in another column

耗尽温柔 提交于 2019-12-04 21:37:57
问题 I don't understand why I can't find a solution for this, since I feel that this is a pretty basic question. Need to ask for help, then. I want to rearrange airquality dataset by month with maximum temp value for each month. In addition I want to find the corresponding day for each monthly maximum temperature. What is the laziest (code-wise) way to do this? I have tried following without a success: require(reshape2) names(airquality) <- tolower(names(airquality)) mm <- melt(airquality, id.vars

Must one `melt` a dataframe before having it `cast`?

有些话、适合烂在心里 提交于 2019-12-04 16:52:39
Must one melt a data frame prior to having it cast? From ?melt : data molten data frame, see melt. In other words, is it absolutely necessary to have a data frame molten prior to any acast or dcast operation? Consider the following: library("reshape2") library("MASS") xb <- dcast(Cars93, Manufacturer ~ Type, mean, value.var="Price") m.Cars93 <- melt(Cars93, id.vars=c("Manufacturer", "Type"), measure.vars="Price") xc <- dcast(m.Cars93, Manufacturer ~ Type, mean, value.var="value") Then: > identical(xb, xc) [1] TRUE So in this case the melt operation seems to have been redundant. What are the

How to avoid NA columns in dcast() output?

折月煮酒 提交于 2019-12-04 04:27:27
问题 How can I avoid NA columns in dcast() output from the reshape2 package? In this dummy example the dcast() output will include an NA column: require(reshape2) data(iris) iris[ , "Species2"] <- iris[ , "Species"] iris[ 2:7, "Species2"] <- NA (x <- dcast(iris, Species ~ Species2, value.var = "Sepal.Width", fun.aggregate = length)) ## Species setosa versicolor virginica NA ##1 setosa 44 0 0 6 ##2 versicolor 0 50 0 0 ##3 virginica 0 0 50 0 For a somewhat similar usecase, table() does have an

Duplicating observations of a dataframe, but also replacing specific variable values in R

隐身守侯 提交于 2019-12-04 03:12:01
问题 I am looking for some advice on some data restructuring. I am collecting some data using Google Forms which I download as a csv file and looks something like the following: # alpha beta option # 6 8, 9, 10, 11 apple # 9 6 pear # 1 6 apple # 3 8, 9 pear # 3 6, 8 lime # 3 1 apple # 2, 4, 7, 11 9 lime The data has two variables (alpha and beta) that each list numbers. For the majority of my data there is only one number in each variable. However, for some observations there can be two, three or