[R] Reshaping a data frame with a series of factors and 23 repeated measures

Ista Zahn istazahn at gmail.com
Tue Mar 30 16:59:55 CEST 2010


Hi Bill,
Here is a reshape package version. The key thing to notice is that you
have multiple pieces of information in your original column names.
These can be split out using the colsplit() function:

# Read in the data
cattle <- read.csv("http://n4.nabble.com/attachment/1745223/0/CattleGrowth.csv",
colClasses="character")
# make the naming scheme consistent
names(cattle)[names(cattle) %in% c("Date", "Days", "Wt")] <-
c("Date.0", "Days.0", "Wt.0")
# melt the data
m.cattle <- melt(cattle, id = c("Steer.ID", "stocker.trt", "Finish.trt"))
# split out variable and time info
m.cattle <- as.data.frame(cbind(colsplit(m.cattle$variable,
split="\\.", names=c("Var", "time")), m.cattle))
# get rid of the now-redundant variable column
m.cattle$variable <- NULL
# cast the data to put variables back in the columns
long.cattle <- cast(m.cattle, ... ~ Var)

Best,
Ista

On Tue, Mar 30, 2010 at 9:34 AM, wclapham <william.clapham at ars.usda.gov> wrote:
>
> Ista,
>
> I have looked at the reshape package and have used Œmelt¹ successfully on
> simpler tables.  I tried it here, but have not been successful.  I think I
> just need to gain experience.  I am loving R and am having a difficult time
> with data structure issues.
>
> I am attaching the data set that I am trying to manipulate.  Ultimately, I
> would like to be able to analyze these data with ANOVA and repeated
> measures, and also be able to plot  growth,  wt * days.  I appreciate any
> help or guidance on references to read that will help me solve my problems.
>
> The data set represents wt over time (starting with days=0, birth day) for
> steers.  Factors include Stockering and Finishing treatments.
>
> Regards,
>
> Bill
>
>
> On 3/29/10 3:39 PM, "Ista Zahn [via R]"
> <ml-node+1695531-1043721504-210803 at n4.nabble.com> wrote:
>
>> Hi Bill,
>> Without an example dataset it's hard to see exactly what you need to
>> do. But you can get started by looking at the documentation for the
>> reshape function (?reshape), and by looking at the reshape package.
>> The reshape package has an associated web page
>> (http://had.co.nz/reshape/) with links to papers and other information
>> to help you get started.
>>
>> Best,
>> Ista
>>
>> On Mon, Mar 29, 2010 at 3:15 PM, wclapham <[hidden email]
>> <http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1695531&i=0>
>> <http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1695531&i=0>
>> > wrote:
>>
>>> >
>>> > I have a data frame that I created using read.table on a csv spreadsheet.
>>> > The data look like the following:
>>> >
>>> > Steer.ID   stocker.trt   Finish.trt  Date   Days Wt ......
>>> >
>>> > Steer.Id, stocker.trt, Finish.trt are factors------ Date, Days, Wt are data
>>> > that are repeated 23 times (wide format).
>>> >
>>> > I want to reshape the data such that I have the correct Steer.ID,
>>> > stocker.trt, Finish.trt identifying all of the repeated measures data in a
>>> > long  format.
>>> >
>>> > I am a newbie at R and need to develop the skill in reshaping data, so that
>>> > I can handle routine problems like described above.
>>> >
>>> > Thanks so much in advance for help or advice.
>>> >
>>> > Bill
>>> > --
>>> > View this message in context:
>>> http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-r
>>> epeated-measures-tp1695500p1695500.html
>>> > Sent from the R help mailing list archive at Nabble.com.
>>> >
>>> > ______________________________________________
>>> > [hidden email]
>>> <http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1695531&i=1>
>>> <http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1695531&i=1>
>>> mailing list
>>> > 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.
>>> >
>>
>>
>
>
>
> --
> View this message in context: http://n4.nabble.com/Reshaping-a-data-frame-with-a-series-of-factors-and-23-repeated-measures-tp1695500p1745223.html
> Sent from the R help mailing list archive at Nabble.com.
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list