简体   繁体   中英

Trouble generating a distribution plot and/or histogram in R.

I have searched high and low for what should be a very simple answer. In brief, while attempting to generate a distribution plot in RI kept receiving an error 'x' must be numeric. I have attempted to isolate the issue and believe I have found it, but have no idea why it is appearing. I have even moved form my initial data set (relatively complex with 200,000 values all between 0 and 1) to a very simple generic data set that still returns the same problems. I'll walk you through what I'm doing and you den tell me why I'm a bit slow.

I load my dataset (its a data frame at this point I believe)

> test <- read.table("/Users/timjenkins/Desktop/Test5hmcR/test.txt", header = T)
> str(test)
'data.frame':   200 obs. of  4 variables:
$ AGE   : int  20 53 44 37 26 41 39 28 33 39 ...
$ HEIGHT: int  176 167 170 173 170 165 174 171 180 166 ...
$ WEIGHT: int  77 56 80 89 71 62 75 68 100 74 ...
$ CHOL  : int  195 250 304 178 206 284 232 152 209 150 ...

> head(test)
 AGE HEIGHT WEIGHT CHOL
1  20    176     77  195
2  53    167     56  250
3  44    170     80  304
4  37    173     89  178
5  26    170     71  206
6  41    165     62  284

I then select my column vector of interest

> weight <- test$weight

then attempt to generate a histogram or density plot

> hist(weight)
Error in hist.default(weight) : 'x' must be numeric
> density(weight)
Error in density.default(weight) : argument 'x' must be numeric

in looking back at the summaries for the column vector it appears that I'm doing something wrong at this step but I can't figure it out...

> head(weight)
NULL
> str(weight)
NULL

I have also tried to force this to be numeric 'as.numeric' but the results still seem a bit messed up...

> w <- as.numeric(weight)
> str(w)
num(0) 
> head(w)
numeric(0)

Any thoughts? I am likely missing something very basic, but I have made many histograms in R and have never run into this problem until today and now it appears regardless of what data set I am working with, which tells me I am doing something very wrong (but at least I'm being consistent). Thanks in advance.

In R, case matters. weight != WEIGHT

Try

weight <- test$WEIGHT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM