If I have a dataframe which is organized like this:
name color likes?
1 sally red 0
2 sally blue 1
3 sally green 1
4 jake red 0
5 jake blue 1
6 jake green 1
7 tom red 1
8 tom blue 0
9 tom green 0
And I want to create a matrix in the form:
red blue green
sally 0 1 1
jake 0 1 1
tom 1 0 0
Are there any built-in commands that might help me do this? Also, I
can't assume that there is an observation for every person-color. In
other words, in the original dataset, there might be some colors for
which sally offered no opinion. In some cases, this may be represented
by NA, in others, it may mean that no row exists for sally for that color.
Thank you!
xtabs is your friend:
xtabs(likes ~ color + name, data=dat)
color
name blue green red
jake 1 1 0
sally 1 1 0
tom 0 0 1
See ?xtabs for more info. Note that I changed the "likes?" column to
just "likes". It is a bad idea to have question marks in variable
names.
Simon.
On Wed, 2009-03-11 at 01:42 -0400, Jennifer Brea wrote:> If I have a dataframe which is organized like this:
>
> name color likes?
> 1 sally red 0
> 2 sally blue 1
> 3 sally green 1
> 4 jake red 0
> 5 jake blue 1
> 6 jake green 1
> 7 tom red 1
> 8 tom blue 0
> 9 tom green 0
>
>
> And I want to create a matrix in the form:
>
> red blue green
> sally 0 1 1
> jake 0 1 1
> tom 1 0 0
>
>
> Are there any built-in commands that might help me do this? Also, I
> can't assume that there is an observation for every person-color. In
> other words, in the original dataset, there might be some colors for
> which sally offered no opinion. In some cases, this may be represented
> by NA, in others, it may mean that no row exists for sally for that color.
>
> Thank you!
>
> ______________________________________________
> 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.
--
Simon Blomberg, BSc (Hons), PhD, MAppStat.
Lecturer and Consultant Statistician
School of Biological Sciences
The University of Queensland
St. Lucia Queensland 4072
Australia
Room 320 Goddard Building (8)
T: +61 7 3365 2506
http://www.uq.edu.au/~uqsblomb
email: S.Blomberg1_at_uq.edu.au
Policies:
1. I will NOT analyse your data for you.
2. Your deadline is your problem.
The combination of some data and an aching desire for
an answer does not ensure that a reasonable answer can
be extracted from a given body of data. - John Tukey.
Hi: Below works but it's extremely ugly and overly complicated. i'm sure
someone else will send you something better and I'll be waiting also.
Also, the way I named the rows and columns works for below but it won't
hold in the general case if you don't have nice ordered names like
you do below. So, use it until we something better.
DF <- read.table(textConnection("name color likes
1 sally red 0
2 sally blue 1
3 sally green 1
4 jake red 0
5 jake blue 1
6 jake green 1
7 tom red 1
8 tom blue 0
9 tom green 0"),header=TRUE,stringsAsFactors=FALSE)
mat <- matrix(sapply(1:nrow(DF),function(.row) {
DF[.row,3]
}),nrow=length(unique(DF$name)),byrow=TRUE,dimnames=list(c(unique(DF$name)),c(unique(DF$color))))
print(mat)
On Wed, Mar 11, 2009 at 12:42 AM, Jennifer Brea wrote:
> If I have a dataframe which is organized like this:
>
> name color likes?
> 1 sally red 0
> 2 sally blue 1
> 3 sally green 1
> 4 jake red 0
> 5 jake blue 1
> 6 jake green 1
> 7 tom red 1
> 8 tom blue 0
> 9 tom green 0
>
>
> And I want to create a matrix in the form:
>
> red blue green
> sally 0 1 1
> jake 0 1 1
> tom 1 0 0
>
>
> Are there any built-in commands that might help me do this? Also, I
> can't assume that there is an observation for every person-color. In
> other words, in the original dataset, there might be some colors for
> which sally offered no opinion. In some cases, this may be
> represented by NA, in others, it may mean that no row exists for sally
> for that color.
>
> Thank you!
>
> ______________________________________________
> 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.
Something like this will work
m<-matrix(df1$likes, nr=3,nc=3,byrow=T)
colnames(m)<-unique(df1$color)
rowlnames(m)<-unique(df1$name)
Sincerly.
Justin BEM
BP 1917 Yaoundé Cameroun
Tél (237) 76043774
________________________________
De : Jennifer Brea <brea@fas.harvard.edu>
À : r-help@r-project.org
Envoyé le : Mercredi, 11 Mars 2009, 6h42mn 29s
Objet : [R] Converting a dataframe to a matrix
If I have a dataframe which is organized like this:
name color likes?
1 sally red 0
2 sally blue 1
3 sally green 1
4 jake red 0
5 jake blue 1
6 jake green 1
7 tom red 1
8 tom blue 0
9 tom green 0
And I want to create a matrix in the form:
red blue green
sally 0 1 1
jake 0 1 1
tom 1 0 0
Are there any built-in commands that might help me do this? Also, I can't
assume that there is an observation for every person-color. In other words, in
the original dataset, there might be some colors for which sally offered no
opinion. In some cases, this may be represented by NA, in others, it may mean
that no row exists for sally for that color.
Thank you!
______________________________________________
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.
[[alternative HTML version deleted]]