R error - Table of extent 0

Jalui picture Jalui · Jan 29, 2018 · Viewed 20.5k times · Source

I'm a complete newbie to R so I'm currently trying to work my way through Youtube videos and books as I need to use R for a paper. I'm working in R Studio.

I'm currently trying to get the frequency of one one of the features in my data. The value can either be 1 or 2 and I want to know how often I have 1 and how often 2.

I'm importing my data like this:

y <- read.table("Auszählung.csv", header = TRUE, sep = ";",   
comment.char ="", fill = TRUE, check.names = TRUE)

And I'm trying to get the frequency like this:

TableInfluncer <- table(y$V4)

Now the problem is that I get < table of extent 0 > as a result.

If I change header = FALSE I get the right values but then I obviously have a third value which is the name of the column that I do not want as one of my values.

So why is it not working when I set the header which is the first row of my table to true?

Answer

AdamO picture AdamO · Jan 29, 2018

header=T uses the first row of the CSV to name the variables. V4 is the name for the 4th variable automatically generated when the header=F. Check the actual names of the dataset by using colnames(y). If you try to access a variable that doesn't exist in a data.frame, R gives you a length 0 nothing or NULL value.