Florian Ahrweiler
2012-Nov-15 16:10 UTC
[R] Adding two different factors to one observation?
In a data frame, I would like to assign two or more factors to one observation. Is it possible? If so, how? Example: In the variable $ dishes, there are several levels: "cup", "plate", "saucer". In my first observation, I see a saucer only. But in the second one, I see a cup and a saucer. In the third, however, there is only a cup, in the fourth a plate only and so forth. I'm an R beginner and I'll be grateful for any help. Florian
Florian, There are a number of different ways to handle data like this. Two that come to my mind are shown below. You could allow each observation to be represented by multiple rows in the data frame: obs dishes 1 1 saucer 2 2 cup 3 2 plate 4 2 saucer 5 3 cup 6 3 plate 7 4 cup 8 4 saucer 9 6 plate 10 6 saucer 11 7 plate 12 8 cup Or you could let each level be its own variable: obs cup plate saucer 1 1 0 0 1 2 2 1 1 1 3 3 1 1 0 4 4 1 0 1 5 5 0 0 0 6 6 0 1 1 7 7 0 1 0 8 8 1 0 0 The arrangement that you choose will likely depend on what you want to do with the data. Jean Florian Ahrweiler <florian.ahrweiler@uni-wh.de> wrote on 11/15/2012 10:10:53 AM:> > In a data frame, I would like to assign two or more factors to one > observation. Is it possible? If so, how? > > Example: In the variable $ dishes, there are several levels: "cup", > "plate", "saucer". In my first observation, I see a saucer only. But in > the second one, I see a cup and a saucer. In the third, however, there > is only a cup, in the fourth a plate only and so forth. > > I'm an R beginner and I'll be grateful for any help. > > Florian[[alternative HTML version deleted]]
Hi, If you want to convert from the first way to the second way suggested by Jean: dat1<-read.table(text=" ?Obs. dishes ?1?? saucer ?2?? cup ?2?? saucer ?3 cup ?4? plate ?",sep="",header=TRUE,stringsAsFactors=TRUE) library(reshape2) dat2<-dcast(dat1,Obs.~dishes,value.var="Obs.") ?dat2[,2:4]<-ifelse(is.na(dat2[,2:4]),0,1) ? dat2 #? Obs. cup plate saucer #1??? 1?? 0???? 0????? 1 #2??? 2?? 1???? 0????? 1 #3??? 3?? 1???? 0????? 0 #4??? 4?? 0???? 1????? 0 A.K. ----- Original Message ----- From: Jean V Adams <jvadams at usgs.gov> To: Florian Ahrweiler <florian.ahrweiler at uni-wh.de> Cc: r-help at r-project.org Sent: Thursday, November 15, 2012 2:50 PM Subject: Re: [R] Adding two different factors to one observation? Florian, There are a number of different ways to handle data like this.? Two that come to my mind are shown below.? You could allow each observation to be represented by multiple rows in the data frame: ? obs dishes 1? ? 1 saucer 2? ? 2? ? cup 3? ? 2? plate 4? ? 2 saucer 5? ? 3? ? cup 6? ? 3? plate 7? ? 4? ? cup 8? ? 4 saucer 9? ? 6? plate 10? 6 saucer 11? 7? plate 12? 8? ? cup Or you could let each level be its own variable: ? obs cup plate saucer 1? 1? 0? ? 0? ? ? 1 2? 2? 1? ? 1? ? ? 1 3? 3? 1? ? 1? ? ? 0 4? 4? 1? ? 0? ? ? 1 5? 5? 0? ? 0? ? ? 0 6? 6? 0? ? 1? ? ? 1 7? 7? 0? ? 1? ? ? 0 8? 8? 1? ? 0? ? ? 0 The arrangement that you choose will likely depend on what you want to do with the data. Jean Florian Ahrweiler <florian.ahrweiler at uni-wh.de> wrote on 11/15/2012 10:10:53 AM:> > In a data frame, I would like to assign two or more factors to one > observation. Is it possible? If so, how? > > Example: In the variable $ dishes, there are several levels: "cup", > "plate", "saucer". In my first observation, I see a saucer only. But in > the second one, I see a cup and a saucer.? In the third, however, there > is only a cup, in the fourth a plate only and so forth. > > I'm an R beginner and I'll be grateful for any help. > > Florian??? [[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.