reshape2

How to shift data by a factor of two months in R?

拈花ヽ惹草 提交于 2021-02-20 19:08:17
问题 I would like to move down my entire data by a factor of two months. For example, if my data starts on Jan 01, i want to move the data in such a way that the data corresponds to March 01. Likewise, November data would become January data for the next year. Here is my sample code DF <- data.frame(seq(as.Date("2001-01-01"), to= as.Date("2003-12-31"), by="day"), A = runif(1095, 0,10), D = runif(1095,5,15)) colnames(DF) <- c("Date", "A", "B") I tried DF$Date <- DF$Date + 61 but this moved the

How to shift data by a factor of two months in R?

那年仲夏 提交于 2021-02-20 19:08:00
问题 I would like to move down my entire data by a factor of two months. For example, if my data starts on Jan 01, i want to move the data in such a way that the data corresponds to March 01. Likewise, November data would become January data for the next year. Here is my sample code DF <- data.frame(seq(as.Date("2001-01-01"), to= as.Date("2003-12-31"), by="day"), A = runif(1095, 0,10), D = runif(1095,5,15)) colnames(DF) <- c("Date", "A", "B") I tried DF$Date <- DF$Date + 61 but this moved the

Melting two sets of columns from wide- to long-form using reshape2 [duplicate]

吃可爱长大的小学妹 提交于 2021-02-19 05:17:05
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed 4 years ago . I am trying to add a time-varying predictor to a long-form dataframe using reshape2::melt but I was wondering if there was a faster way to do it. Here is the toy data in wide form. There are three measures of an outcome variable (session1, session2, and session3) taken at different visits/time points. The duration between

R calculating grouped frequency table with percentage [duplicate]

天大地大妈咪最大 提交于 2021-02-11 12:29:29
问题 This question already has answers here : Calculate Percentage for each time series observations per Group in R (2 answers) Closed 4 years ago . Giving the following data.frame , I would like to calculate the occurance of each variable of VAR and the percentage of these occurence by the grouping variable GROUP : GROUP<-c("G1","G2","G1","G2","G3","G3","G1") VAR<-c("A","B","B","A","B","B","A") d<-data.frame(GROUP,VAR) With table() , I get a nice frequency table, counting the occurences of all

How to reshape a wider data.frame to longer data.frame in R? [duplicate]

半世苍凉 提交于 2021-02-10 22:15:51
问题 This question already has answers here : Transpose and Merge columns in R [duplicate] (3 answers) Reshaping data.frame from wide to long format (9 answers) Closed 7 months ago . I was playing with pivot_longer and pivot_wider but probably am missing something. I have a data.frame like D_Wider and would like to convert it to something like D_longer . any way forward? library(tidyverse) D_Wider <- data.frame(A = 15, S = 10, D = 25, Z = 16) Desired Output D_Longer <- data.frame(Stations = c("A",

Insert Column Name into its Value using R

我是研究僧i 提交于 2021-02-08 08:52:38
问题 I need to insert Column Name, Department, into its value. i have code like here: Department <- c("Store1","Store2","Store3","Store4","Store5") Department2 <- c("IT1","IT2","IT3","IT4","IT5") x <- c(100,200,300,400,500) Result <- data.frame(Department,Department2,x) Result The expected result is like: Department <- c("Department_Store1","Departmentz_Store2","Department_Store3","Department_Store4","Department_Store5") Department2 <- c("Department2_IT1","Department2_IT2","Department2_IT3",

Insert Column Name into its Value using R

好久不见. 提交于 2021-02-08 08:52:05
问题 I need to insert Column Name, Department, into its value. i have code like here: Department <- c("Store1","Store2","Store3","Store4","Store5") Department2 <- c("IT1","IT2","IT3","IT4","IT5") x <- c(100,200,300,400,500) Result <- data.frame(Department,Department2,x) Result The expected result is like: Department <- c("Department_Store1","Departmentz_Store2","Department_Store3","Department_Store4","Department_Store5") Department2 <- c("Department2_IT1","Department2_IT2","Department2_IT3",

which.min within reshape2's dcast()?

萝らか妹 提交于 2021-01-29 08:31:47
问题 I would like to extract the value of var2 that corresponds to the minimum value of var1 in each building-month combination. Here's my (fake) data set: head(mydata) # building month var1 var2 #1 A 1 -26.96333 376.9633 #2 A 1 165.38759 317.3993 #3 A 1 47.46345 271.0137 #4 A 2 73.47784 294.8171 #5 A 2 107.80130 371.7668 #6 A 2 10.16384 308.7975 Reproducible code: ## create fake data set: set.seed(142) mydata1 = data.frame(building = rep(LETTERS[1:5],6),month = sort(rep(1:6,5)),var1=rnorm(30,50

What code does a task like the reshape2 package in a base reshape function?

不羁的心 提交于 2021-01-29 06:00:45
问题 I'm learning reshape function(base R) that works like reshape2 package of hadley wickham. I wrote the code below using reshape2 package. melt(iris, id.vars = 'Species') The result : column names are Species , variable, value and entire number of the result is 600 I wrote this code reshape(iris, idvar = 'Species', direction = 'long') But show error message Error in reshape(iris, idvar = "Species", direction = "long") : "no 'reshapeWide' attribute, must specify 'varying' How to see the same

One-way ANOVA for each sub-group in a melted data frame

ε祈祈猫儿з 提交于 2021-01-28 17:58:30
问题 I have a very large data set that requires individual one-way ANOVAs for multiple species on 4 different treatments with several measurements. Usually I just make separate excel spread sheets for each species and run ANOVAs on each, looping through each measurement column, but this is very time consuming. Is it possible to use a single spread sheet and run ANOVAs on the melted data? Or perhaps there is another reshape option I could use? Data: structure(list(Species = c("A", "A", "A", "A", "A