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)
Best wishes,
Angulo