Thank you Phil, your method worked great!
Again, I was about to write back and add some facts -- all of the columns
need to be 'character'... when I tried my and Jonathan's solutions with
columns that were factors, I got errors.
Here's what happened when I tried to use the 'dcast' method on my actual
(not contrived) data with factors:
------------------
>county_sibley <- subset(tmp, county_id == 72)
>county_sibley <- subset(county_sibley, prct_std ==
"GIBBON")[,c("prct_std","prct_name","year")]
>county_sibley$prct_name <- factor(as.character(county_sibley$prct_name))
>county_sibley$prct_std <- factor(county_sibley$prct_std)
>county_sibley$year <- factor(county_sibley$year)
>print(county_sibley)
prct_std prct_name year
3515 GIBBON GIBBON 2002
7631 GIBBON GIBBON 2006
9085 GIBBON GIBBON 2010
>str(county_sibley)
'data.frame': 3 obs. of 3 variables:
$ prct_std : Factor w/ 1 level "GIBBON": 1 1 1
$ prct_name: Factor w/ 1 level "GIBBON": 1 1 1
$ year : Factor w/ 3 levels "2002","2006",..: 1 2 3
county_sibley_wide <- dcast(county_sibley, prct_std ~ year, value_var =
"prct_name")
Error in names(data) <- array_names(res$labels[[2]]) :
'names' attribute [3] must be the same length as the vector [1]
------------------
Converting all used columns to character made the 'cast' and 'dcast'
solution work, so that should be noted.
Thanks to everybody,
Matt
On Wed, Dec 8, 2010 at 8:53 PM, Phil Spector <[email protected]>wrote:
> Another way, using just base R is
>
> reshape(df,idvar='y',timevar='x',v.names='z',direction='wide')
>>
> y z.A z.B z.C
> 1 D a b c
> 4 E d e f
> 7 F g h i
>
> - Phil Spector
> Statistical Computing Facility
> Department of Statistics
> UC Berkeley
> [email protected]
>
>
>
>
> On Wed, 8 Dec 2010, Matthew Pettis wrote:
>
> Hi Jonathan,
>>
>> Thank you very much! I was about to recall this after I found this
>> discussion (https://stat.ethz.ch/pipermail/r-help/2008-June/164440.html),
>> but I think your solution is more tidy.
>>
>> Thank you very much!
>> Matt
>>
>> On Wed, Dec 8, 2010 at 8:33 PM, Jonathan Christensen <[email protected]
>> >wrote:
>>
>> Matt,
>>>
>>> library(reshape2)
>>> wide.df <- dcast(df, y ~ x)
>>>
>>> Works great for me.
>>>
>>> Jonathan
>>>
>>>
>>> On Wed, Dec 8, 2010 at 7:26 PM, Matthew Pettis <[email protected]
>>> >
>>> wrote:
>>>
>>>> Hi,
>>>>
>>>> I was wondering if there is an easy way that I am missing for turning a
>>>>
>>> long
>>>
>>>> dataframe into a wide one. Below is sample code that will make what I
>>>>
>>> have
>>>
>>>> and, in comments, the form of what I want:
>>>>
>>>> # Have: dataframe like 'df'
>>>> df <- expand.grid( x=LETTERS[1:3], y=LETTERS[4:6])
>>>> df$z <- letters[1:length(df[,1])]
>>>>
>>>> # Want: data.frame that has following form:
>>>> # A B C
>>>> # D a b c
>>>> # E d e f
>>>> # F g h i
>>>>
>>>>
>>>> I looked at 'xtabs' and 'cast' from reshape/reshape2, but unless I'm
>>>> misunderstanding something, these will work only for the 'z' column
>>>> being
>>>> numeric, not textual. Is there an easy way to do this with 'z' being
>>>> textual rather than numeric?
>>>>
>>>> tia,
>>>> Matt
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> [email protected] 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.
>>>>
>>>>
>>>
>>
>>
>> --
>> Seven Deadly Sins (Gandhi):
>> - Wealth without work - Politics without principle
>> - Pleasure without conscience - Commerce without morality
>> - Science without humanity - Worship without sacrifice
>> - Knowledge without character
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [email protected] 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.
>>
>>
--
Seven Deadly Sins (Gandhi):
- Wealth without work - Politics without principle
- Pleasure without conscience - Commerce without morality
- Science without humanity - Worship without sacrifice
- Knowledge without character
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.