AbdeAMNR
AbdeAMNR

Reputation: 145

Error in `[.data.frame`(ozone, , 2) : undefined columns selected

I wanted to Represent scatter on a graph by using plot() function and passing it a data which is a file in my local file system, but I'm getting this error, I tried to set names for the header with colnames() function but same error

    ozone <- read.table("C:/Users/Abde/Downloads/donnees_rennes_O3.txt",header=T,sep="\t");
head(mydata);
Date......O3......T12......T15......Ne12......N12......S12......E12......W12......Vx......O3v
1    19960422      63.6      13.4      15      7      0      0      3      0      9.35      95.6
2    19960429      89.6      15      15.7      4      3      0      0      0      5.4      100.2
3    19960506      79      7.9      10.1      8      0      0      7      0      19.3      105.6
4  19960514      81.2      13.1      11.7      7      7      0      0      0      12.6      95.2
5     19960521      88      14.1      16      6      0      0      0      6      -20.3      82.8
6 19960528      68.4      16.7      18.1      7      0      3      0      0      -3.69      71.4

plot(O3,T12,data=mydata,xlab="T12",ylab="O3");

>Error in plot(O3, T12, data = ozone, xlab = "T12", ylab = "O3") : 
object 'O3' not found

Upvotes: 1

Views: 4019

Answers (2)

AbdeAMNR
AbdeAMNR

Reputation: 145

this code works perfect :

mydata <- read.table("C:/Users/Abde/Downloads/newData.txt" , header=TRUE , sep=";")
head(mydata)
x <- mydata[,3]
y <- mydata[,2]
plot(x,y,data=mydata,xlab="T12",ylab="O3")

here is some few lines of the data I'm using:

"Date";"O3";"T12";"T15";"Ne12";"N12";"S12";"E12";"W12";"Vx";"O3v"
"19960422";63.6;13.4;15;7;0;0;3;0;9.35;95.6
"19960429";89.6;15;15.7;4;3;0;0;0;5.4;100.2
"19960506";79;7.9;10.1;8;0;0;7;0;19.3;105.6
"19960514";81.2;13.1;11.7;7;7;0;0;0;12.6;95.2
"19960521";88;14.1;16;6;0;0;0;6;-20.3;82.8
"19960528";68.4;16.7;18.1;7;0;3;0;0;-3.69;71.4
"19960605";139;26.8;28.2;1;0;0;3;0;8.27;90
"19960612";78.2;18.4;20.7;7;4;0;0;0;4.93;60
"19960619";113.8;27.2;27.7;6;0;4;0;0;-4.93;125.8
"19960627";41.8;20.6;19.7;8;0;0;0;1;-3.38;62.6
"19960704";65;21;21.1;6;0;0;0;7;-23.68;38
"19960711";73;17.4;22.8;8;0;0;0;2;-6.24;70.8
"19960719";126.2;26.9;29.5;2;0;0;4;0;14.18;119.8
"19960726";127.8;25.5;27.8;3;0;0;5;0;13.79;103.6

Upvotes: 0

user5249203
user5249203

Reputation: 4648

The columns in text file are separated by spaces and not tabs. However, it is not a single space but around 6 spaces between each column.

In R, sep = " ", checks for one space. But, with sep ="", refers to any lenght of white space. And, you need to pass the values of x and y, not just the column name.

The following code works.

mydata <-
  read.table(
    "C:/PathToYourFile/donnees_rennes_O3.txt",
    sep = "" ,
    header = T ,
    na.strings = "",
    stringsAsFactors = F
  )
head(mydata)
plot(x = mydata$T12,y = mydata$O3, xlab = "T12", ylab = "03")

Upvotes: 1

Related Questions