2
Hello, good afternoon!
I’m finding it difficult to use tidyverse, to perform a stacking function + summarise(soma=sum(value)) + pop = base%>%pivot_longer(cols=(),names_to(),values_to())%>%group_by()%>%summarise(soma=sum(value))%>%spread(variable,m). When using this code by modifying only the function within summarise(), of sum() for mean() or sd(), always worked, now to sum(), does not work, it does not sum, just repeats the value.
To check, I used reshape2::dcast(plyr::ddply(reshape2::melt())) that was the function I always had the habit of using, in addition to confronting the function sum() applied to the values in a subset() determined.
I would like to help you to check how to use by tidyverse, preferably without those exits from "summarise() ... (override with .groups argument)".
Below follows the base, and the codes:
base<-data.frame(expand.grid(FAT1=1:4,FAT2=1:2,FAT3=1:2,AVA=1:6,REP=1:3),
VAR1=runif(3*96,0,1),
VAR2=runif(3*96,-1.5,0),
VAR3=runif(3*96,0,2.5))
require(reshape2)
require(plyr)
require(tidyverse)
mbase<-base %>% pivot_longer(cols=all_of(c("VAR1","VAR2","VAR3")),names_to = "variable",values_to = "value")
dcast(ddply(mbase, .(FAT1,FAT2,FAT3,AVA,variable), summarise, soma=sum(value)),FAT1*FAT2*FAT3*AVA~variable,fun.aggregate = sum,value.var ="soma" )
# (FAT1=1,FAT2=1,FAT3=1,AVA=1)$VAR1 = 1.704
sum(subset(base,FAT1==1 & FAT2==1 & FAT3==1 & AVA==1)[,"VAR1"])
# = 1.704
base %>% pivot_longer(cols=all_of(c("VAR1","VAR2","VAR3")),names_to = "variable",values_to = "value")%>%
group_by(FAT1,FAT2,FAT3,AVA,variable) %>% summarise(soma = sum(value)) %>% spread(variable,soma)
Can you give an example of how you want the final answer? Because from what I understand your code is right, the result of the formula with
tidyverseis the same as with thedcast, although the impression is not exactly the same.– Jorge Mendes
You’ve seen that even I couldn’t answer, because one hour it was working, then it stopped, it stopped so much that I had to generate this question, and now, it worked :'/ , R things that make us ashamed. Anyway, I will test the solutions of Rui Barradas, to suppress the messages!.
– Jean Karlos