Hello, how do you create a histogram with a data frame? 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,...)) hist(mydata) does not work. Many thanks. PR
Hello, it looks like this is what you want. year <- c(1970:1980)> snow <- sample(10,length(year),replace=T) > snow[1] 3 3 3 4 8 3 2 6 6 6 10> year[1] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980> mydata <- data.frame(year,snow) > attach(mydata) > barplot(snow,names.arg=year,las=2)for a histogram of snow you might try something like this hist(snow,breaks=c((min(snow)-0.5):(max(snow)+0.5))) Felix (who is a newbie himself, so excuse me please if i didnt get your problem) Am Mittwoch, 4. Februar 2004 12:41 schrieb Philippe de Rochambeau:> Hello, > > how do you create a histogram with a data frame? > > 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,...)) > > hist(mydata) does not work. > > Many thanks. > > PR > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Dear Philippe, I suppose that you want a histogram for snow.cover and not for year. There are several ways to proceed; two are hist(mydata$snow.cover) and attach(mydata) hist(snow.cover) More generally, it's a good idea to read the introductory manual that comes with R (or a book that introduces R). See, in particular, the section on lists and data frames in the Introduction to R. I hope that this helps, John At 12:41 PM 2/4/2004 +0100, Philippe de Rochambeau wrote:>Hello, > >how do you create a histogram with a data frame? > >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,...)) > >hist(mydata) does not work. > >Many thanks.----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox
You don't make a histogram with a data.frame. You have to pass a vector of numeric values to hist(). If you want to make a histogram using a *column* of a df, you have to subset the df in the call to hist. hist(mydata[,1]) -or- hist(mydata[,"year"]) hist(mydata[,2]) - or- hist(mydata[,"snow.cover"]) HTH, Jim James W. MacDonald Affymetrix and cDNA Microarray Core University of Michigan Cancer Center 1500 E. Medical Center Drive 7410 CCGC Ann Arbor MI 48109 734-647-5623>>> Philippe de Rochambeau <philippe at wwphi.net> 02/04/04 06:41AM >>> >Hello,>how do you create a histogram with a data frame?>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,...))>hist(mydata) does not work.>Many thanks.>PR______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Philippe de Rochambeau wrote:> Hello, > > how do you create a histogram with a data frame? > > 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,...)) > > hist(mydata) does not work.I guess you are not going to create a histogram but a barplot as in: barplot(mydata$snow.cover, mydata$year, names = mydata$year) Uwe Ligges> Many thanks. > > PR > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
It's not world shattering but for the record the following is not true:> -----Original Message----- > From: John Fox [mailto:jfox at mcmaster.ca] > (snip) > In particular, although using with() is perhaps less ambiguous, it is > necessary to repeat it for each command.You can group as many commands as you like, as in> df <- data.frame(x=1:10, y=1:10) > with(df, {print(x)+ print(y)}) [1] 1 2 3 4 5 6 7 8 9 10 [1] 1 2 3 4 5 6 7 8 9 10 Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}