0

I have a data frame defined by “Data” and two subsets of this data set (scale1, scale2). I want to plot this scales by gender and then by country, without having to define scales every time. I know that is possible in R, and may be a newbie question, but I just can’t find the logic to do it. I have searched it but I can’t find what I want. If someone can give me a clue for that data managment (not for plotting), I would be very grateful.

I know with that simple DataFrame maybe could be easier defining subsets everytime, but my original Dataframe has a lot of items and it is difficult operate this way.

Data<- data.frame(item1=c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, NA, 5, NA, NA), 
                  item2=c(1, 2, 2, 4, 1, 1, 2, 3, 5, 5, NA, NA, NA, NA),
                  item3=c(1, 2, 2, 4, 1, 1, 2, 3, 5, 5, NA, NA, NA, NA),
                  item4=c(1, 2, 2, 4, 1, 1, 4, 3, 1, 5, NA, 3, NA, NA),
                  item5=c(1, 5, 2, 4, 2, 1, 2, 3, 5, 5, NA, NA, 1, NA),
                  item6=c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, NA, 5, NA, NA), 
                  item7=c(1, 2, 1, 5, 1, 2, 2, 3, 5, 5, NA, NA, NA, NA),
                  item8=c(1, 4, 2, 4, 3, 1, 2, 8, 5, 5, NA, NA, NA, NA),

                  gender=c(1, 2, 1, 2, 2,1, 2, 1, 2, 1, 1, 2, 1, 2), 
                  country=c(1, 2, 3, 3, 1,1, 2, 1, 3, 1, 3, 2, 1, 2))

scale1 <- subset(Data, select=c(item1, item2, item3, item4))
scale2 <- subset(Data, select=c(item5, item6, item7, item8))

Now for plotting I'm using this instrucions, but I'm pretty sure there is another better way to do that:

 womandata <- Data[ which(Data$gender== "1"), ]

scale1F <-subset(womandata, select= c( item1, item2, item3, item4))
scale2F <-subset(womandata, select= c( item5, item6, item7, item8))



mandata <- Data[ which(Data$gender== "2"), ]

scale1M <-subset(mandata, select= c( item1, item2, item3, item4))
scale2M <-subset(mandata, select= c( item5, item6, item7, item8))

par(mfrow=c(2,1)) 

boxplot(scale1F, xlab="", xaxt = "n", col="gray", main="Woman")
text(1:34, par("usr")[1], 
     srt=45, pos=1, xpd=TRUE, offset=-1)

boxplot(scale1M, xlab="", xaxt = "n", col="gray", main ="Man")
text(1:34, par("usr")[1],
     srt=45, pos=1, xpd=TRUE, offset=-1)

enter image description here

Best wishes,

Angulo

  • Your subsets are identical. – Roland Nov 10 '14 at 10:37
  • i didn't realised when copy-paste, thanks. – Ariadna Angulo Nov 10 '14 at 10:43
  • I'm unsure about how you do the plotting. Please add this for your example. – Roland Nov 10 '14 at 10:52
  • @Roland Edited, I hope is better explained now :) – Ariadna Angulo Nov 10 '14 at 10:59
  • I'm still not getting the whole picture. What is the basis for the selected subsets? Do you want independent plots for a pre-defined list of subsets? Or all possible subsets? Or would you like facetted plots? What exactly does "better way" mean in your context? – Roland Nov 10 '14 at 11:56
  • I have 30 items and 6 scales (of 9000 observations) For each scale, I want to plot boxplots giving a condition (for example gender), without subseting everytime the data (because if i do that, probably I'll get mistaken when writting). I would like to define scales once, and don't do it again on the whole analysis (For exemple, I would like to say. Scale 1 is defined by item1,item2, item3 and item4. And then when I want to plot, only order to plot that scale by a condition (without having to define it again)). – Ariadna Angulo Nov 10 '14 at 12:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64628/discussion-between-ariadna-angulo-and-roland). – Ariadna Angulo Nov 10 '14 at 12:15

1 Answers1

1

I'm still not sure of your aim, but maybe this helps:

female <- Data$gender == 1
scale1 <- names(Data) %in% c("item1", "item2", "item3", "item4")

boxplot(Data[female, scale1], xlab="", xaxt = "n", col="gray", main="Woman")
boxplot(Data[!female, scale1], xlab="", xaxt = "n", col="gray", main="Man")
Roland
  • 127,288
  • 10
  • 191
  • 288
  • that could work when only are two values :) so it helps.I've tried that logical and try to do country==1/2/3 and it also works, so it's perfect:) Thank you – Ariadna Angulo Nov 11 '14 at 07:45