eliavs
eliavs

Reputation: 2356

why do i get "Error unepexted symbol in"

I am trying to read many csv files
When i read one of the files not using a value, it works

I have a list of file named list.of.files like so:

#[1] "2013ARI.EVN" "2013ATL.EVN" "2013CHN.EVN" "2013CIN.EVN" "2013COL.EVN"
#[6] "2013LAN.EVN" "2013MIA.EVN" "2013MIL.EVN" "2013NYN.EVN" "2013PHI.EVN"
#[11] "2013PIT.EVN" "2013SDN.EVN" "2013SFN.EVN" "2013SLN.EVN" "2013WAS.EVN"

so i can make a for loop i tested assign() like this

name<-substr(list.of.files[1],1,7)
assign(name,read.csv(paste(name,".csv",sep="")))
summary(ARI2013)

#Error in summary(ARI2013) : object 'ARI2013' not found

what is wrong with the way i am assigning?

Upvotes: 0

Views: 75

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60984

assign creates an R object with the name you pass as a string. You passed 2013ARI as input, but refer to ARI2013 in summary. The error you see is simply because you did not use the correct object name. The second problem here is that you start your variable name with a number, which does not work well in R. You could use get to obtain the object nonetheless:

> substr("2013ARI.EVN", 1, 7)
[1] "2013ARI"
> assign(substr("2013ARI.EVN", 1, 7), 1)
> summary(get("2013ARI"))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      1       1       1       1       1       1 

Do note however, using assign to creates groups of objects is not the best way in R. I would put the set of csv files in a list:

list_of_files = sprintf("%s.csv", substr(c("2013ARI.EVN", "2013ATL.EVN"), 1, 7))
list_of_data_frames = lapply(list_of_files, read.csv)

Upvotes: 1

Related Questions