Hello, 1) What is the difference between a "data frame" (J H Maindonald, Using R, p. 12) and a "vector"? In Using R, the author asks the reader to enter the following data in a data frame, which I will call "mydata": year snow.cover 1970 6.5 1971 12.0 1972 14.9 1973 10.0 1974 10.7 1975 7.9 ... mydata=data.frame(year=c(1970,...),snow.cover=c(6.5,...)) 2) How to you retrieve say, snow.cover's second data item? mydata[1][2] does not work, neither does mydata[1,2]. hist(mydata[1,2]) does not work. How would you create a histogram with the above data? In a French statistics book, the author provides the following data: Group A Number: 35 Mean:27 Group B Number: 42 Mean:24 and asks: "what is the mean of the group constituted by the reunion of the two groups?" The answer is of course (27 x 35) + (24 x 42) / 77 3) Is there a way to compute this mean in R (apart from doing the above operation, of course) if you have two sets of data? 4) How do you set class limits in R, for instance 10-20 21-31 etc. 5) How do you determine quartiles in R? Is there a way to determine the "semi-inter-quartile deviation" ("?cart semi-inter-quartile" in French)? Many thanks! Philippe de Rochambeau
r-help-bounces at stat.math.ethz.ch wrote on 04/02/2004 12:33:15:> Hello, > > 1) What is the difference between a "data frame" (J H Maindonald, Using > R, p. 12) and a "vector"?a vector is a sequence of data of a certain kind ("of a certain mode"). You can have a vector of numbers> myvector <- c(1,2,3,4,5,6) > mode(a)[1] "numeric" or a vector of character strings> myvector <- c("have", "a", "look", "at", "Manuals", "section", "on","CRAN")> mode(myvector)[1] "character" or vectors of other kinds (e.g. logical). a data frame is what you would call 'une matrice de donn?es' in French. In R a matrix can contain only one type of data (e.g. numerical data or character strings) whilst a data.frame can contain different data types in different columns (one per column, though). These things are explained more clearly in "An Introduction to R", that you can find on CRAN in the Manuals section.> In Using R, the author asks the reader to enter the following data in a > data frame, which I will call "mydata": > > year snow.cover > 1970 6.5 > 1971 12.0 > 1972 14.9 > 1973 10.0 > 1974 10.7 > 1975 7.9 > ... > > mydata=data.frame(year=c(1970,...),snow.cover=c(6.5,...)) > > 2) How to you retrieve say, snow.cover's second data item? mydata[1][2] > does not work, neither does mydata[1,2].mydata[i,j] will give you the element of the ith row and jth column mydata[2,2] will give what you want. [question on histogram]> > In a French statistics book, the author provides the following data: > > Group A Number: 35 Mean:27 > Group B Number: 42 Mean:24 > > and asks: "what is the mean of the group constituted by the reunion of > the two groups?" > > The answer is of course (27 x 35) + (24 x 42) / 77 > > 3) Is there a way to compute this mean in R (apart from doing the above > operation, of course) if you have two sets of data? >R is a very flexible programming language, so you can do a lot of what you can imagine and more. If you have the two sets, there is no need to do this, just concatenate these two sets and calculate the mean. If you want to make your own function for doing this, it could be done as follows: myfun <- function(set1, set2){ set1and2 <- c(set1, set2) overallmean <- mean(set1and2) return(overallmean) } Then use this new user-defined function with two vectors of your own, say a and b> a <- c(1,2,3) > b <- c(4,5,6) > myfun(a, b)[1] 3.5 If you only have the data of the exercise in the statistics textbook, you can use the weighted.mean function of R:> weighted.mean(c(27,24), w=c(35,42))[1] 25.36364 which is correct> (27*35+24*42)/77[1] 25.36364> 4) How do you set class limits in R, for instance > > 10-20 > 21-31 > etc.For this you could use the cut() function Type ?cut at the R prompt and the help page on this function will show up.> 5) How do you determine quartiles in R? Is there a way to determine the > "semi-inter-quartile deviation" ("?cart semi-inter-quartile" in > French)? >I know of IQR, but am not sure it is what you want. Read its help page by typing ?IQR I hope that this helps, Tobias
Hi,> -----Original Message----- > From: Philippe de Rochambeau [SMTP:philippe at wwphi.net] > Sent: Wednesday, February 04, 2004 12:33 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Various newbie questions > > year snow.cover > 1970 6.5 > 1971 12.0 > 1972 14.9 > 1973 10.0 > 1974 10.7 > 1975 7.9 > ... > > mydata=data.frame(year=c(1970,...),snow.cover=c(6.5,...)) > > 2) How to you retrieve say, snow.cover's second data item? mydata[1][2] > does not work, neither does mydata[1,2]. >year <- 1970:1975 snow.cover <- c(6.5,12,14.9,10,10.7,7.9) mydata <- data.frame(cbind(year,snow.cover)) # retrieving 2nd column: mydata[,2] # retrieving 3rd row: mydata[3,] # snow.cover's second data-item: mydata[2,2] # or: mydata$snow.cover[2]> In a French statistics book, the author provides the following data: > > Group A Number: 35 Mean:27 > Group B Number: 42 Mean:24 > > and asks: "what is the mean of the group constituted by the reunion of > the two groups?" > > The answer is of course (27 x 35) + (24 x 42) / 77 > > 3) Is there a way to compute this mean in R (apart from doing the above > operation, of course) if you have two sets of data? >numbers <- c(35,42) arithmeans <- c(27,24) weighted.mean(arithmeans,numbers) Just a general remark: the first thing you should do (as is written in the R-Posting-Guide) is consulting the available online help of R. For example, you could use ?vector ?data.frame to get an answer for your first question. If you don't know actually what you are looking for, you can also enter a search string and, optionally, the package. In the case of question 3, you could have written: help.search("mean", package="base") and R would have returned several commands - among them also "weighted.mean" Best, Roland +++++ This mail has been sent through the MPI for Demographic Research. Should you receive a mail that is apparently from a MPI user without this text displayed, then the address has most likely been faked. If you are uncertain about the validity of this message, please check the mail header or ask your system administrator for assistance.
Am Mittwoch, 4. Februar 2004 12:33 schrieb Philippe de Rochambeau:> Hello, > > 1) What is the difference between a "data frame" (J H Maindonald, Using > R, p. 12) and a "vector"?a vector looks like this: 1 2 3 4 5 ... a data.frame has two dimensions, the rows and the coloumns> > In Using R, the author asks the reader to enter the following data in a > data frame, which I will call "mydata": > > year snow.cover > 1970 6.5 > 1971 12.0 > 1972 14.9 > 1973 10.0 > 1974 10.7 > 1975 7.9 > ... > > mydata=data.frame(year=c(1970,...),snow.cover=c(6.5,...)) > > 2) How to you retrieve say, snow.cover's second data item? mydata[1][2] > does not work, neither does mydata[1,2].mydata[2,1] should do the trick: to get a specific cell out of a data.frame you use this: name.of.frame[row,coloumn] try help(apply), this should make the logic behind that a little more clearer> hist(mydata[1,2]) does not work. How would you create a histogram with > the above data?a histogram gives, as far as i kinow, the frequency of the values of one variable, so you might try hist(mydata$snow) to get the histogram for snow try help(hist) for more information> > In a French statistics book, the author provides the following data: > > Group A Number: 35 Mean:27 > Group B Number: 42 Mean:24 > > and asks: "what is the mean of the group constituted by the reunion of > the two groups?" > > The answer is of course (27 x 35) + (24 x 42) / 77 > > 3) Is there a way to compute this mean in R (apart from doing the above > operation, of course) if you have two sets of data?lets say you have two vectory called A and B with your given options, you would get their mean by mean(c(A,B))> 4) How do you set class limits in R, for instance > > 10-20 > 21-31 > etc.try help(cut)> 5) How do you determine quartiles in R? Is there a way to determine the > "semi-inter-quartile deviation" ("?cart semi-inter-quartile" in > French)?this question i did not understand for getting a start in R i recommend "R for Beginners" by Emmanuel Paradis, which can be found after following the link "contributed" on www.r-project.org Felix Eschenburg