Erica
July 23, 2020, 5:22pm
1
when I use the code
df%>% pivot_wider(id_cols=id, names_from="group", values_from="value")
I get a dataframe where each group column contains a list of values from the value column.... when I use
df%>% pivot_wider(id_cols=id, names_from="group", values_from="value", values_fn=sum())
I get Error in values_fn[[value]] : subscript out of bounds
What am I missing? Thanks!
joels
July 23, 2020, 5:26pm
2
Remove the parentheses from sum()
and it should work:
df%>%
pivot_wider(id_cols=id, names_from="group", values_from="value",
values_fn=sum)
Erica
July 23, 2020, 6:08pm
3
I get a different error when I do it that way:
Error in values_fn[[value]] : object of type 'builtin' is not subsettable
Any idea what it means?
joels
July 23, 2020, 7:07pm
4
I think we'll need a reproducible example (data and code that we can copy and paste to reproduce the problem you're having) to figure out what's going on. For now, here's an example that works:
library(tidyverse)
iris %>%
pivot_wider(names_from=Species, values_from=where(is.numeric),
values_fn=sum) %>%
as.data.frame()
#> Sepal.Length_setosa Sepal.Length_versicolor Sepal.Length_virginica
#> 1 250.3 296.8 329.4
#> Sepal.Width_setosa Sepal.Width_versicolor Sepal.Width_virginica
#> 1 171.4 138.5 148.7
#> Petal.Length_setosa Petal.Length_versicolor Petal.Length_virginica
#> 1 73.1 213 277.6
#> Petal.Width_setosa Petal.Width_versicolor Petal.Width_virginica
#> 1 12.3 66.3 101.3
Created on 2020-07-23 by the reprex package (v0.3.0)
1 Like
system
Closed
August 13, 2020, 7:07pm
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.