1

Hopefully this makes sense. I have one column in my dataset that has multiple entries of one of three size category (read in the data as characters), "(0,1.88]", "(1.88,4]", and "(4,10]". I would to combine all of my entries together by plot (another column in the dataset), totaling the response for each size category in its own column.

Ideally, I'm trying to take data which has multiple responses in each Plot and end up with one total response for each plot, divided by size category. I'm hoping to get something like this:

Plot     Total Response for (0,1.88]     Total Response for (1.88,4]     Total Response for (4,10]

Here is the head of my data. Not all of it is needed, only Plot, ounces, and tuber.diam. tuber.diam has the entries grouped into size categories.

head(newChippers)
  Plot ounces Height Shape Area plot variety rate block width length tuber.oz.bin tuber.diam
1 2422   1.31   1.22 26122 3237  242  Lamoka    3     4  1.65   1.70        (0,4]   (0,1.88]
2 2422   2.76   1.56 27853 5740  242  Lamoka    3     4  2.20   2.24        (0,4]   (1.88,4]
3 2422   1.62   1.31 24125 3721  242  Lamoka    3     4  1.53   1.95        (0,4]   (0,1.88]
4 2422   3.37   1.70 27147 6498  242  Lamoka    3     4  2.17   2.48        (0,4]   (1.88,4]
5 2422   3.19   1.70 27683 6126  242  Lamoka    3     4  2.22   2.34        (0,4]   (1.88,4]
6 2422   2.83   1.53 27356 6009  242  Lamoka    3     4  2.00   2.53        (0,4]   (1.88,4]

Here is what I currently have for making the new dataset:

YieldSizeProfileDiameter <- newChippers %>%
  group_by(Plot) %>% 
  summarize(totalOz = sum(Weight),
            Diameter.0.1.88  = (tuber.diam("(0,1.88]")),
            Diameter.1.88.4  = (tuber.diam("    (1.88,4]")),
            Diameter.4.10  = (tuber.diam("  (4,10]"))) 

I get the following error code:

Error in x[[n]] : object of type 'closure' is not subsettable

Any help would be very much appreciated! Again, I'm very sorry if I've explained it poorly or made it too complicated. If any additional information is needed, I can try to provide it. Thank you!

Tom
  • 377
  • 3
  • 13
  • Please share a little bit of sample data. Best is if you use `dput` so it is copy-pasteable, e.g., `dput(your_data[1:10,])`. Also please edit your desired output so that it has more than just column names---putting in the values corresponding to your sample data is best of all. – Gregor Thomas Dec 09 '19 at 19:59
  • You also might want to look at the R-FAQ on converting data [from long to wide format](https://stackoverflow.com/q/5890584/903061). This, possibly combined with a grouped sum, is maybe what you want? – Gregor Thomas Dec 09 '19 at 20:01
  • Where does `tuber.diam` come from? Can you include your expected output? I don't understand what you mean by turning one column into 3—splitting strings? Or reshaping the data frame? – camille Dec 09 '19 at 20:01
  • Thank you for the help everyone! I just added the current dataset with a small explanation to go along with it. I'm not sure how to add anything more for desired output, as I don't know what the data will look like other than the headers. – Tom Dec 09 '19 at 20:08
  • I think you may be looking for `tidyr::pivot_wider`. Also see here: https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Axeman Dec 09 '19 at 21:03

1 Answers1

0

I have revised your code. I assume your variable weight is the same as variable ounce as there is no weight variable in newChippers your data data. I use weight here as in your code:

YieldSizeProfileDiameter <- newChippers %>%
  group_by(Plot, tuber.diam) %>% 
  summarize(totalOz = sum(Weight)) %>%
  pivot_wider(names_from = tuber.diam, values_from = totalOz)
YieldSizeProfileDiameter

I have not tested the code on my side as I do not have the data.

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27