[R] reshape() not dropping varaibles

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sun Dec 10 18:21:24 CET 2023


It would be nice to see what OP wanted to end up with, but the link contained input data to experiment with.

The first problem is that if you are not interested in working with the whole set of columns then you need to only give a data frame with the columns you want to work with:

dta.wide <- reshape(
  Data[, c( "SURVEYDATE", "COMMON_NAME", "TOTAL_CATCH" ) ]
  , direction = "wide"
  , idvar = "SURVEYDATE"
  , timevar = "COMMON_NAME"
  , v.names = "TOTAL_CATCH"
)

Second, there are multiple rows of TOTAL_CATCH for each combination of SURVEYDATE and COMMON_NAME. reshape does not do math on the data... you have to resolve that yourself first by choosing how you want to turn a bunch of numbers into one... in this case I chose to sum them up:

Data.agg <- aggregate(
  TOTAL_CATCH ~ SURVEYDATE + COMMON_NAME
  , data = Data
  , FUN = sum
)
# the aggregate calculation implicitly leaves out columns you don't specify

dta.wide <- reshape(
  Data.agg
  , direction = "wide"
  , idvar = "SURVEYDATE"
  , timevar = "COMMON_NAME"
  , v.names = "TOTAL_CATCH"
)
Data.wide

On December 10, 2023 7:35:17 AM PST, Bert Gunter <bgunter.4567 using gmail.com> wrote:
>Posting a few rows, say 5, of your data using dput() along with the result
>that you would like to get for those rows would help get you a quicker and
>more accurate response, I believe. This is as suggested by the posting
>guide, linked below, which you should read if you have not already.
>
>-- Bert
>
>
>
>On Sun, Dec 10, 2023 at 2:38 AM Bob O'Hara <rni.boh using gmail.com> wrote:
>
>> Hi all!
>>
>> I1m trying to re-format some data from long to wide format with reshape().
>> Specifically, the data has SURVEYDATE, which I want to be in the rows, and
>> COMMON_NAME which should be the columns. The entries should be TOTAL_CATCH.
>> The data has a bunch of other variables, which can be ignored.
>>
>> When I run reshape(), it includes all of the variables, not just
>> TOTAL_CATCH:
>>
>> Data <- read.csv("
>>
>> https://conservancy.umn.edu/bitstream/handle/11299/227105/fish_data_raw.csv?sequence=6&isAllowed=y
>> ")
>> Data.wide <- reshape(Data, direction = "wide",
>>                 idvar = "SURVEYDATE", timevar = "COMMON_NAME",
>>                 v.names = "TOTAL_CATCH")
>> names(Data.wide)
>>
>> I tried with the example on the help page, which works fine:
>>
>> # this works
>> Indometh$thing <- 1:nrow(Indometh)
>> wide <- reshape(Indometh, direction = "wide", idvar = "Subject",
>>                 timevar = "time", v.names = "conc", sep= "_")
>> names(wide)
>>
>> There are some obvious work-arounds and alternatives, but it would be nice
>> to have this sorted. Can anyone help?
>>
>> Bob
>>
>> Bob
>>
>> --
>> Bob O'Hara
>> Institutt for matematiske fag
>> NTNU
>> 7491 Trondheim
>> Norway
>>
>> Mobile: +47 915 54 416
>> Journal of Negative Results - EEB: www.jnr-eeb.org
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list