Re: [R] re shape package - use one cast() instead of many

2009-05-05 Thread jwg20

Thanks for your help! I wasn't sure what the margins variable did, but I'm
beginning to understand. I'm almost there, but with my data (and with ff_d)
I tried to margin over two variable names, however it only does one of them.
So with ff_d I set margins=c("treatment","variable"); however I only ever
get 1_(all) 2_(all) and 3_(all)... never something like (all)_painty. (This
also happens for margins=TRUE)

To further exemplify, with my data I have the call to cast() as:
cast(data.melted,Subject~CogStat+Animacy,mean,margins=c("CogStat","Animacy"))

which results in:

  Subject COG_aCOG_i COG_(all)nCOG_anCOG_i nCOG_(all)
1 100  794. 676.5556  728.0833  810.7778  798.4103   800.7292
...

I would like additionally to get (all)_i (all)_a. It seems to just apply the
margin to the first part of the formula before "+" (i.e. I can change it to
Animacy+CogStat and get a_COG,a_nCOG,   a_(all), etc.)


hadley wrote:
> 
> On Tue, May 5, 2009 at 3:03 PM, jwg20  wrote:
>>
>> I have a data set that I'm trying to melt and cast in a specific way
>> using
>> the reshape package. (I'll use the ff_d dataset from reshape so I don't
>> have
>> to post a toy data set here. )
>>
>> Lets say I'm looking for the interaction of treatment with each type of
>> "variable" in ff_d. Using the command below gets me this. Subject will
>> get a
>> column and each treatment type by each variable will also get a column
>> with
>> values for each.
>>
>> cast(ff_d, subject~treatment+variable)
>>   subject 1_potato 1_buttery 1_grassy 1_rancid 1_painty 2_potato
>> 2_buttery
>>   3_painty
>> 1        3       18        18       18       18       18       18      
>>  18
>>    18
>> ...
>>
>> Now, if I want to look at just the  the values for each variable by
>> subject
>> I can run the following command.
>> cast(ff_d, subject~variable)
>>   subject potato buttery grassy rancid painty
>> 1        3     54      54     54     54     54
>> ...
>>
>> What I'm wondering now, is run one cast() call and get both of these in
>> one
>> data.frame? Essentially, the values for each separate "condition" and
>> interactions between them? cast() doesn't let me repeat variable names as
>> that's what I first tried.  Right now, i'm just running two separate
>> cast()
>> calls and cbinding/merging them together. Is there a better way?
> 
> Have a look at the margins argument.
> 
> Hadley
> 
> 
> -- 
> http://had.co.nz/
> 
> __
> R-help@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.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/reshape-package---use-one-cast%28%29-instead-of-many-tp23394916p23395785.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] re shape package - use one cast() instead of many

2009-05-05 Thread jwg20

I have a data set that I'm trying to melt and cast in a specific way using
the reshape package. (I'll use the ff_d dataset from reshape so I don't have
to post a toy data set here. )

Lets say I'm looking for the interaction of treatment with each type of
"variable" in ff_d. Using the command below gets me this. Subject will get a
column and each treatment type by each variable will also get a column with
values for each.

cast(ff_d, subject~treatment+variable)
   subject 1_potato 1_buttery 1_grassy 1_rancid 1_painty 2_potato 2_buttery
  3_painty
13   1818   18   18   18   1818 
   
   18
...

Now, if I want to look at just the  the values for each variable by subject
I can run the following command.
cast(ff_d, subject~variable)
   subject potato buttery grassy rancid painty
13 54  54 54 54 54
...

What I'm wondering now, is run one cast() call and get both of these in one
data.frame? Essentially, the values for each separate "condition" and
interactions between them? cast() doesn't let me repeat variable names as
that's what I first tried.  Right now, i'm just running two separate cast()
calls and cbinding/merging them together. Is there a better way?  
-- 
View this message in context: 
http://www.nabble.com/reshape-package---use-one-cast%28%29-instead-of-many-tp23394916p23394916.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Calculating First Occurance by a factor

2009-04-01 Thread jwg20

Thanks! That did it. I should have seen that I needed the comma! 



hadley wrote:
> 
>> I tried messing with the line df$FixTime[which.min(df$FixInx)] changing
>> it
>> to df[which.min(df$FixInx)] or adding new lines with the additional
>> columns
>> that I want to include, but nothing seemed to work. I'll admit I only
>> have a
>> mild understanding of what is going on with the function .fun. :-)
> 
> You probably want:
> 
> df[which.min(df$FixInx), ]
> 
> Hadley
> 
> -- 
> http://had.co.nz/
> 
> __
> R-help@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.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Calculating-First-Occurance-by-a-factor-tp22789964p22834747.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Calculating First Occurance by a factor

2009-04-01 Thread jwg20

I have another question regarding ddply. In my actual data.frame, I have many
other column variables corresponding to the type of the trial. I'm wondering
if I can have ddply include those in firstfixtime as well.

I tried messing with the line df$FixTime[which.min(df$FixInx)] changing it
to df[which.min(df$FixInx)] or adding new lines with the additional columns
that I want to include, but nothing seemed to work. I'll admit I only have a
mild understanding of what is going on with the function .fun. :-)




Mike Lawrence wrote:
> 
> I discovered Hadley Wickham's "plyr" package last week and have found
> it very useful in circumstances like this:
> 
> library(plyr)
> 
> firstfixtime = ddply(
>.data = data
>, .variables = c('Sub','Tr','IA')
>, .fun <- function(df){
>df$FixTime[which.min(df$FixInx)]
>}
> )
> 
>> On Mon, Mar 30, 2009 at 3:40 PM, jwg20  wrote:
>>>
>>> I'm having difficulty finding a solution to my problem that without
>>> using a
>>> for loop. For the amount of data I (will) have, the for loop will
>>> probably
>>> be too slow. I tried searching around before posting and couldn't find
>>> anything, hopefully it's not embarrassingly easy.
>>>
>>> Consider the data.frame, Data,  below
>>>
>>> Data
>>> Sub Tr  IA   FixInx  FixTime
>>> p1   t1  1    1        200
>>> p1   t1  2    2        350
>>> p1   t1  2    3        500
>>> p1   t1  3    4        600
>>> p1   t1  3    5        700
>>> p1   t1  4    6        850
>>> p1   t1  3    7        1200
>>> p1   t1  5    8        1350
>>> p1   t1  5    9        1500
>>>
>>> What I'm trying to do is for each unique IA get the first occurring
>>> FixTime.
>>> This will eventually need to be done by each Trial (Tr) and each Subject
>>> Number (Sub). FixInx is essentially the number of rows in a trial. The
>>> resulting data.frame is below.
>>>
>>> Sub Tr  IA  FirstFixTime
>>> p1   t1  1   200
>>> p1   t1  2   350
>>> p1   t1  3   600
>>> p1   t1  4   850
>>> p1   t1  5   1350
>>>
>>> Here is the solution I have now.
>>>
>>> agg = aggregate(data$FixInx, list(data$Sub, data$Tr, data$IA), min) #get
>>> the
>>> minimum fix index by Sub, Tr, and IA... I can use this min fix index to
>>> pull
>>> out the desired fixtime
>>>
>>> agg$firstfixtime = 0 # new column for results
>>>
>>> for (rown in 1:length(rownames(agg))){ #cycle through rows and get each
>>> data$firstfixtime from FixTime in matching rows
>>>  agg$firstfixtime[rown] = as.character(data[data$Tr == agg$Group.2[rown]
>>> &
>>> data$Sub == agg$Group.1[rown] & data$IA == agg$Group.3[rown] &
>>> data$FixInx
>>> == agg$x[rown], ]$FixTime)
>>> }
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Calculating-First-Occurance-by-a-factor-tp22789964p22789964.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> __
>>> R-help@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.
>>>
> 
> 
> 
> -- 
> Mike Lawrence
> Graduate Student
> Department of Psychology
> Dalhousie University
> 
> Looking to arrange a meeting? Check my public calendar:
> http://tinyurl.com/mikes-public-calendar
> 
> ~ Certainty is folly... I think. ~
> 
> __
> R-help@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.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Calculating-First-Occurance-by-a-factor-tp22789964p22829525.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[R] Calculating First Occurance by a factor

2009-03-30 Thread jwg20

I'm having difficulty finding a solution to my problem that without using a
for loop. For the amount of data I (will) have, the for loop will probably
be too slow. I tried searching around before posting and couldn't find
anything, hopefully it's not embarrassingly easy.  

Consider the data.frame, Data,  below

Data
Sub Tr  IA   FixInx  FixTime
p1   t1  11200
p1   t1  22350
p1   t1  23500
p1   t1  34600
p1   t1  35700
p1   t1  46850
p1   t1  371200
p1   t1  581350
p1   t1  591500

What I'm trying to do is for each unique IA get the first occurring FixTime.
This will eventually need to be done by each Trial (Tr) and each Subject
Number (Sub). FixInx is essentially the number of rows in a trial. The
resulting data.frame is below.

Sub Tr  IA  FirstFixTime
p1   t1  1   200
p1   t1  2   350
p1   t1  3   600
p1   t1  4   850
p1   t1  5   1350

Here is the solution I have now.  

agg = aggregate(data$FixInx, list(data$Sub, data$Tr, data$IA), min) #get the
minimum fix index by Sub, Tr, and IA... I can use this min fix index to pull
out the desired fixtime

agg$firstfixtime = 0 # new column for results

for (rown in 1:length(rownames(agg))){ #cycle through rows and get each
data$firstfixtime from FixTime in matching rows 
  agg$firstfixtime[rown] = as.character(data[data$Tr == agg$Group.2[rown] &
data$Sub == agg$Group.1[rown] & data$IA == agg$Group.3[rown] & data$FixInx
== agg$x[rown], ]$FixTime)
}
-- 
View this message in context: 
http://www.nabble.com/Calculating-First-Occurance-by-a-factor-tp22789964p22789964.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.