[R] combining elements in a data frame

Dennis Murphy djmuser at gmail.com
Fri Jul 15 11:07:23 CEST 2011


Hi:

Q1: Try something like

# Formula interface works for R-2.11.0 and later versions
RTavg <- aggregate(RT ~ word, data = alldat, FUN = mean)
merge(CCCW, RTavg, by.x = row.names(CCCW), by.y = 'word', all = TRUE)

If the merge doesn't work (which is entirely possible), you might want
to define a variable word in CCCW first and then try again with
something like
merge(CCCW, RTavg, by = 'word', all = TRUE)

IIRC, all = TRUE keeps words from both data frames filling missing
values with NA, all.x keeps everything from the first argument of
merge() and all.y keeps everything from the second argument. If you
omit the option, it returns only the words that occur in both data
frames. From your description, it appears you want all.x = TRUE where
x = CCCW. See ?merge for specific details.

Q2:  See ?table and ?ftable

Utterly untested code in the absence of a reproducible example, so
caveat emptor.

Dennis

On Thu, Jul 14, 2011 at 9:17 PM, Lee Averell
<Lee.Averell at newcastle.edu.au> wrote:
> Hi all,
>        I have 2 data frames the first contains a list with repeats of words and an associated response time (RT) measure for each word. The second is a tabulation of each unique word and other information such as the amount and of responses for each word. I need to determine the mean RT for each word and add that as a column in the second data frame.
> Any help would be appreciated
> Cheers
> Lee
>
> Data frame 1
>
>> head(alldat)
>     s expt session cycle trial left.right freq concr    word   rt resp Response correct corrResp
> 121 1a    a       1    C1     1          1   lf    hc pianist 1529  old       hi   FALSE      new
> 122 1a    a       1    C1     2          1   hf    hc   sweat 1518  new       hi    TRUE      new
> 123 1a    a       1    C1     3          1   lf    lc carnage 1046  old       hi    TRUE      old
> 124 1a    a       1    C1     4          1   lf    hc   nymph 1142  old       hi    TRUE      old
> 125 1a    a       1    C1     5          1   hf    lc    hank 1487  new       hi    TRUE      new
> 126 1a    a       1    C1     6          1   lf    hc   waist 1199  new       hi    TRUE      new
>    respType
> 121        s
> 122        s
> 123        s
> 124        s
> 125        s
> 126        s
>>
>
> Data frame 2
>
>> head(CCCW)
>        FALSE TRUE    CC    propCC lo hi
> abode       2   11  TRUE 0.8461538  4  9
> abyss       1   12  TRUE 0.9230769  2 11
> accord      2   11  TRUE 0.8461538  2 11
> account     0    0 FALSE       NaN  0  0
> acre        4    9  TRUE 0.6923077  4  9
> adage       0    0 FALSE       NaN  0  0
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list