My data table comb
(dput
output below) has the structure below. I want to dcast it like this
comb_wide <- dcast(comb, scenarios qName hemName ~ PWC_cutoff, value.var = sum)
R returns this error message
Error in setattr(ans, "names", c(lhsnames, allcols)) :
'names' attribute [7] must be the same length as the vector [3]
I can't figure out what attribute 7 and vector 3 refer to.
> str(comb)
Classes ‘data.table’ and 'data.frame': 96 obs. of 5 variables:
$ scenarios : chr "historical_1991_2010" "ssp126_2041_2060" "ssp126_2081_2100" "ssp585_2041_2060" ...
$ qName : chr "q1" "q1" "q1" "q1" ...
$ hemName : Factor w/ 2 levels "NH","SH": 1 1 1 1 1 1 1 1 1 1 ...
$ sum : num 0 0 0 0 0 ...
$ PWC_cutoff: num 20 20 20 20 20 20 40 40 40 40 ...
- attr(*, ".internal.selfref")=<externalptr>
dput output
structure(list(scenarios = c("historical_1991_2010", "ssp126_2041_2060",
"ssp126_2081_2100", "ssp585_2041_2060", "ssp585_2081_2100", "aglabor",
"historical_1991_2010", "ssp126_2041_2060", "ssp126_2081_2100",
"ssp585_2041_2060", "ssp585_2081_2100", "aglabor", "historical_1991_2010",
"ssp126_2041_2060", "ssp126_2081_2100", "ssp585_2041_2060", "ssp585_2081_2100",
"aglabor", "historical_1991_2010", "ssp126_2041_2060", "ssp126_2081_2100",
"ssp585_2041_2060", "ssp585_2081_2100", "aglabor", "historical_1991_2010",
"ssp126_2041_2060", "ssp126_2081_2100", "ssp585_2041_2060", "ssp585_2081_2100",
"aglabor", "historical_1991_2010", "ssp126_2041_2060", "ssp126_2081_2100",
"ssp585_2041_2060", "ssp585_2081_2100", "aglabor", "historical_1991_2010",
"ssp126_2041_2060", "ssp126_2081_2100", "ssp585_2041_2060", "ssp585_2081_2100",
"aglabor", "historical_1991_2010", "ssp126_2041_2060", "ssp126_2081_2100",
"ssp585_2041_2060", "ssp585_2081_2100", "aglabor", "historical_1991_2010",
"ssp126_2041_2060", "ssp126_2081_2100", "ssp585_2041_2060", "ssp585_2081_2100",
"aglabor", "historical_1991_2010", "ssp126_2041_2060", "ssp126_2081_2100",
"ssp585_2041_2060", "ssp585_2081_2100", "aglabor", "historical_1991_2010",
"ssp126_2041_2060", "ssp126_2081_2100", "ssp585_2041_2060", "ssp585_2081_2100",
"aglabor", "historical_1991_2010", "ssp126_2041_2060", "ssp126_2081_2100",
"ssp585_2041_2060", "ssp585_2081_2100", "aglabor", "historical_1991_2010",
"ssp126_2041_2060", "ssp126_2081_2100", "ssp585_2041_2060", "ssp585_2081_2100",
"aglabor", "historical_1991_2010", "ssp126_2041_2060", "ssp126_2081_2100",
"ssp585_2041_2060", "ssp585_2081_2100", "aglabor", "historical_1991_2010",
"ssp126_2041_2060", "ssp126_2081_2100", "ssp585_2041_2060", "ssp585_2081_2100",
"aglabor", "historical_1991_2010", "ssp126_2041_2060", "ssp126_2081_2100",
"ssp585_2041_2060", "ssp585_2081_2100", "aglabor"), qName = c("q1",
"q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1",
"q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1",
"q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1",
"q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1", "q1",
"q1", "q1", "q1", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3",
"q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3",
"q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3",
"q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3", "q3",
"q3", "q3", "q3", "q3", "q3", "q3", "q3"), hemName = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("NH",
"SH"), class = "factor"), sum = c(0, 0, 0, 0, 0, 746051, 0, 0,
0, 0, 35, 746051, 0, 141, 341, 598, 25672, 746051, 50171, 72535,
75671, 82293, 200119, 746051, 0, 0, 0, 0, 0, 137649, 0, 0, 0,
0, 0, 137649, 0, 0, 0, 7, 26595, 137649, 39501, 54149, 54192,
62361, 92355, 137649, 0, 0, 0, 0, 0, 746051, 0, 0, 0, 0, 28991,
746051, 3909, 63236, 75858, 99751, 341094, 746051, 365996, 462080,
464957, 488522, 569331, 746051, 0, 0, 0, 0, 0, 137649, 0, 0,
0, 0, 0, 137649, 0, 1, 2, 110, 8023, 137649, 14720, 18365, 18447,
21514, 35848, 137649), PWC_cutoff = c(20, 20, 20, 20, 20, 20,
40, 40, 40, 40, 40, 40, 60, 60, 60, 60, 60, 60, 80, 80, 80, 80,
80, 80, 20, 20, 20, 20, 20, 20, 40, 40, 40, 40, 40, 40, 60, 60,
60, 60, 60, 60, 80, 80, 80, 80, 80, 80, 20, 20, 20, 20, 20, 20,
40, 40, 40, 40, 40, 40, 60, 60, 60, 60, 60, 60, 80, 80, 80, 80,
80, 80, 20, 20, 20, 20, 20, 20, 40, 40, 40, 40, 40, 40, 60, 60,
60, 60, 60, 60, 80, 80, 80, 80, 80, 80)), row.names = c(NA, -96L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x13783aee0>)
CodePudding user response:
As indicated in a comment by dww , the column name 'sum
' confused dcast. I changed the column name to sumvals
and the dcast statement to
comb_wide <- dcast(comb, scenarios qName hemName ~ PWC_cutoff, value.var = "sumvals")
CodePudding user response:
I guess you need to put the last term into quotes, so try this one:
comb_wide <- dcast(comb, scenarios qName hemName ~
PWC_cutoff,value.var = 'sum')