incorrect number of dimensions error

1,038 views
Skip to first unread message

Ayala Allon

unread,
Jul 1, 2014, 11:15:52 AM7/1/14
to israel-r-...@googlegroups.com
Hi,

I'm trying to write a function that computes the mean for the second or third columns in a csv file. When I run the function I get the next error for  data.column <- data[,2] or data.column <- data[,3] : incorrect number of dimensions error. However when I load the csv file and run these lines outside of the function I don't get an error. I can't figure out what am I missing.

This is the function:
pollutantmean<- function(directory,pollutant,id) {
setwd(directory)
if (id < 9) {
data<- paste("00", id, ".csv", sep = "")
else if (id > 10 && id < 100) {
data<- paste("0", id, ".csv", sep = "")
}
read.csv(data)
if (pollutant == "sulfate") {
data.column <- data[,2]   ###### this is the line for which I get the error
} else if (pollutant == "nitrate") {
data.column <- data[,3]   ###### this is the line for which I get the error
}
bad <- is.na(data.column)
x <- mean(data.column[!bad])
#x <- mean(good)
list(directory = getwd(), head.data = head(data), mean.column=x)


Any help will be greatly appreciated,
Ayala

amit gal

unread,
Jul 1, 2014, 4:10:49 PM7/1/14
to israel-r-...@googlegroups.com
I guess the "data" variable" contains the name of the file, and hence has not dimension.
or did I miss something?



--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

מיכאל דורמן

unread,
Jul 2, 2014, 3:47:29 AM7/2/14
to israel-r-...@googlegroups.com
As Amit has said, assignment of the CSV file is missing. Instead of "read.csv(data)" should be, perhaps, "data <- read.csv(data)"

Ayala Allon

unread,
Jul 2, 2014, 4:40:35 AM7/2/14
to israel-r-...@googlegroups.com


On Tuesday, July 1, 2014 11:10:49 PM UTC+3, Amit Gal wrote:
I guess the "data" variable" contains the name of the file, and hence has not dimension.
or did I miss something?

Thanks Amit!!
Ayala 

On Tue, Jul 1, 2014 at 6:15 PM, Ayala Allon <ayala...@gmail.com> wrote:
Hi,

I'm trying to write a function that computes the mean for the second or third columns in a csv file. When I run the function I get the next error for  data.column <- data[,2] or data.column <- data[,3] : incorrect number of dimensions error. However when I load the csv file and run these lines outside of the function I don't get an error. I can't figure out what am I missing.

This is the function:
pollutantmean<- function(directory,pollutant,id) {
setwd(directory)
if (id < 9) {
data<- paste("00", id, ".csv", sep = "")
else if (id > 10 && id < 100) {
data<- paste("0", id, ".csv", sep = "")
}
read.csv(data)
if (pollutant == "sulfate") {
data.column <- data[,2]   ###### this is the line for which I get the error
} else if (pollutant == "nitrate") {
data.column <- data[,3]   ###### this is the line for which I get the error
}
bad <- is.na(data.column)
x <- mean(data.column[!bad])
#x <- mean(good)
list(directory = getwd(), head.data = head(data), mean.column=x)


Any help will be greatly appreciated,
Ayala

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.

Ayala Allon

unread,
Jul 2, 2014, 4:41:07 AM7/2/14
to israel-r-...@googlegroups.com
Thanks Michael!
Reply all
Reply to author
Forward
0 new messages