0

I don't know if the method of loading csv files has to do with the issues that I'm encountering, but it's my only lead. The thing is that I have been successfully using a great package by @Max Conway (here), but today I got stuck generating some exploratory plots as illustrated below:

install.packages('gsheet')
library(gsheet)
data <- gsheet2tbl(("https://docs.google.com/spreadsheets/d/1TagYu-g8C1EA-RBNtWewgDUeG0Uq-lfPKn1EHebeNmE/edit?usp=sharing"))

head(data)
Source: local data frame [6 x 3]

      yield nitrogen phosphorus
1 0.8274156       no         no
2 3.6126275       no         no
3 2.6192581       no         no
4 1.7412190       no         no
5 0.6590589       no         no
6 0.4891107       no         no

attach(data)
plot(nitrogen,yield)

Error in plot.window(...) : need finite 'xlim' values
    In addition: Warning messages:
    1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
    2: In min(x) : no non-missing arguments to min; returning Inf
    3: In max(x) : no non-missing arguments to max; returning -Inf

So I ran a str to see how the data had been imported:

str(data)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   40 obs. of  3 variables:
 $ yield     : num  0.827 3.613 2.619 1.741 0.659 ...
 $ nitrogen  : chr  "no" "no" "no" "no" ...
 $ phosphorus: chr  "no" "no" "no" "no" ...

OK... Didn't look like a data.frame, but I figured that had an easy fix:

data <- data.frame(data)
> str(data)
'data.frame':   40 obs. of  3 variables:
 $ yield     : num  0.827 3.613 2.619 1.741 0.659 ...
 $ nitrogen  : chr  "no" "no" "no" "no" ...
 $ phosphorus: chr  "no" "no" "no" "no" ..

At this point (and after visually examining the df), it seemed like the problem had to be fixed. As a precaution, I didn't attach the data:

plot(data$nitrogen,data$yield)

plot(data$nitrogen,data$yield)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

Same problem...

What is R trying to say? What am I doing wrong?

Community
  • 1
  • 1
Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114
  • 1
    Just change nitrogen to factor: `data$nitrogen <- factor(data$nitrogen)`. and plot with `plot(yield ~ nitrogen, data = data)` or `plot(data$nitrogen, data$yield)` – bergant May 14 '15 at 23:28
  • You know... I thought about doing this... But I couldn't believe it could be a necessary step, until - after your comment, and thank you! - it dawned on me that character is different from factor... Now it makes sense... The csv is read, but some objects may be misinterpreted as character values... – Antoni Parellada May 14 '15 at 23:52
  • @bergant, What would be an elegant way of correcting this default, multiple columns at a time? – Antoni Parellada May 15 '15 at 00:05
  • 1
    Use `read.csv(text = gsheet2text(url, format = "csv"))` instead of `gsheet2tbl`. – bergant May 15 '15 at 00:11

0 Answers0