0

I am very new to R and am struggling to import data from a .csv file.

Specifically, I am trying to pull 31 responses, some "1", some "2", some "3", and some "4" from the .csv file. This data is under the header Q21

So I tried doing:

c <- mydata["Q21"] #because (mydata$21) gave an "$ operator is invalid for atomic vectors response 

But that just returned [1] NA

I am looking for 31 integer responses.

No idea what I'm doing wrong. Would truly appreciate any and all help as I get on my feet with this language.

EDIT:

str(mydata) yields chr "/Users/charles/Documents/Work/Survey/CSV/Acme_Company.csv"

I did use read.csv(mydata) and it produced everything, it seemed to have column headings like Q20.

When I typed in c <- mydata$Q21 (that was a typo I wrote before), I got: Error in mydata$Q21 : $ operator is invalid for atomic vectors

mydata[1:3,] Error in mydata[1:3, ] : incorrect number of dimensions

is.data.frame(mydata) [1] FALSE

colnames(mydata) NULL

mydata[, "Q21"] Error in mydata[, "Q21"] : incorrect number of dimensions

So I don't know-- mydata isn't a data frame, right? So what should I do if I want to use the information in it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Charlie
  • 9
  • 3
  • 2
    **Post a reproducible example. Use `dput()`.** Just show us a few lines of your dataframe, say `mydata[1:3,]`. – smci Jun 19 '15 at 19:45
  • 1
    How did you import your data? It doesn't sound like `mydata` is a data.frame. Did you use `read.csv()` or something? – MrFlick Jun 19 '15 at 19:49
  • Show us `str(mydata)` . Also check `is.data.frame(mydata)`, sounds like it's false, so your `read.csv()` (or whatever command you used) didn't return you a dataframe - please post the read command. – smci Jun 19 '15 at 19:53

1 Answers1

0

Sounds like mydata is not a dataframe but a vector; so read.csv() didn't return you a dataframe. (Post the command you used to read the data, then post the output of str(mydata) ).

(mydata$21) gave an "$ operator is invalid for atomic vectors response


That's your error, you meant mydata$Q21. mydata$21 is attempting to access a column named 21 not Q21. Read your error messages more closely ;-)

mydata["Q21"] just returned [1] NA

First, check if mydata actually has a column called Q21 or not. Show us colnames(mydata).

Second, to get a slice of a column, you need mydata[, "Q21"] , you absolutely need the leading comma, which signifies that the thing after the comma is column names or indices.

smci
  • 32,567
  • 20
  • 113
  • 146
  • I don't think the error message suggests that the column name is an issue, as normally that would return `NULL` (although not for $21 which would be a different error if $"21" wasn't used). It seems to just suggest that `mydata` is a vector, matrix or something. – ping Jun 19 '15 at 19:51
  • @ping well I thought of suggesting that mydata might not be a dataframe and that `read.csv()` didn't return OP a dataframe, but a vector. But I thought I'd give them the benefit of the doubt. – smci Jun 19 '15 at 20:02