xyplot does not appear to recognize a data file with .txt extension in the same location as setwd()

#set the working file directory to this source file location which also has the OTC.IM.Plasma.20.txt file
plot.RealData <- xyplot(CV~Time,
type = c("p"), # p is for "points" so it shows open circles,
data = OTC.IM.Plasma.20.txt,
col = "red")
The resulting error is Error in eval(substitute(groups), data, environment(x)) :
object 'OTC.IM.Plasma.20.txt' not found
The contents of the .txt file are as follows:
Time CV
0.5 2.02
0.5 2.74
0.5 1.3
1 5.38
1 6.4
1 4.36
2 5.44
2 5.83
2 5.05
3 5.94
3 6.29
3 5.59
4 6
4 6.57
4 5.43
5 5.7
5 6.28
5 5.12
6 5.36
6 6.06
6 4.66
24 1.74
24 2.32
24 1.16
30 1.02
30 1.42
30 0.62
48 0.6
48 0.9
48 0.3
54 0.38
54 0.55
54 0.21
72 0.32
72 0.56
72 0.08
78 0.18
78 0.34
78 0.02

I am not aware of any R functions that read in data from a file in the way you expect this function to read in OTC.IM.Plasma.20.txt. Is the xyplot function from the lattice package? Usually, you read in the data with a function like read.csv()

MyData <- read.csv("OTC.IM.Plasma.20.txt", sep = " ")

Then, you can use the MyData object in the xyplot call

xyplot(CV~Time,
type = c("p"), 
data = MyData,
col = "red")

library(lattice)
library(latticeExtra)
MyData <- read.csv("OTC.IM.Plasma.20.txt", sep = " ")
xyplot(CV~Time,
type = c("p"),
data = MyData,
col = "red")

This time the error is
Error in eval(i, data, env) : object 'CV' not found

Try looking at the structure of MyData. It was probably not read in correctly. You can see its structure with

str(MyData)

You can see its column names with

colnames(MyData)

You can see its contents as a table with

View(MyData)

My guess is that the column separator was not set correctly and MyData only has one column. My code used a space as the separator. Do you know what the true separator is? If a space did not work, try a tab.

MyData <- read.csv("OTC.IM.Plasma.20.txt", sep = "\t")
1 Like

MyData <- read.csv("OTC.IM.Plasma.20.txt", sep = "\t")
The data appears as
Time
CV
...
...
plot.RealData <- xyplot(CV~Time,
type = c("p"), # p is for "points" so it shows open circles,
data = MyData,
col = "red")

With the Resulting in Error in eval(i, data, env) : object 'CV' not found

Do you know what the separator is in your txt file?

What is the output of

dput(head(MyData,10))

dput(head(MyData,10))
structure(list(X.File.name..OTC.IM.Plasma.20.txt = structure(c(1L,
4L, 3L, 2L, 57L, 56L, 13L, 26L, 13L, 28L), .Label = c("#Dataset from Immelman and Dreyer, 1981",
"#Dataset reformatted for Berkeley Madonna by Zhoumeng Lin February 18, 2017",
"#Original model in acslX by Zhoumeng Lin et al. 2015 ",
"#PDOSEIM = 20 mg/kg", "0.02", "0.08", "0.18", "0.21", "0.3",
"0.32", "0.34", "0.38", "0.5", "0.55", "0.56", "0.6", "0.62",
"0.9", "1", "1.02", "1.16", "1.3", "1.42", "1.74", "2", "2.02",
"2.32", "2.74", "24", "3", "30", "4", "4.36", "4.66", "48", "5",
"5.05", "5.12", "5.36", "5.38", "5.43", "5.44", "5.59", "5.7",
"5.83", "5.94", "54", "6", "6.06", "6.28", "6.29", "6.4", "6.57",
"72", "78", "CV", "Time"), class = "factor")), row.names = c(NA,
10L), class = "data.frame")

This is what the first 10 rows of your data look like:

 MyData
                                             X.File.name..OTC.IM.Plasma.20.txt
1                                      #Dataset from Immelman and Dreyer, 1981
2                                                          #PDOSEIM = 20 mg/kg
3                        #Original model in acslX by Zhoumeng Lin et al. 2015 
4  #Dataset reformatted for Berkeley Madonna by Zhoumeng Lin February 18, 2017
5                                                                         Time
6                                                                           CV
7                                                                          0.5
8                                                                         2.02
9                                                                          0.5
10                                                                        2.74

These five rows

X.File.name..OTC.IM.Plasma.20.txt
1                                      #Dataset from Immelman and Dreyer, 1981
2                                                          #PDOSEIM = 20 mg/kg
3                        #Original model in acslX by Zhoumeng Lin et al. 2015 
4  #Dataset reformatted for Berkeley Madonna by Zhoumeng Lin February 18, 2017

look like meta data that should be skipped. The skip argument of read.csv allows you to do that. I cannot tell for sure what the column separator is but tab is not correct.
Try

MyData <- read.csv("OTC.IM.Plasma.20.txt", sep = " ", skip = 5)

You should look at these types of files with a plain text editor such as Notepad on Windows. That will allow you to clearly see the file structure and set up read.csv()

Here's what I did to complete it, thanks to your guidance !! It worked. 5 stars for you.
MyData <- read.csv("OTC.IM.Plasma.20.txt", sep = "\t", skip = 5)
head(MyData)
X = MyData$Time
Y = MyData$CV
plot.RealData <- plot(X, Y, type = c("p"), col = 'blue', main = "Actual Data", xlab = c('Time (hours)'), ylab = c('CV (mg/mL)'))

A clearer option for skipping comment lines would be:
MyData <- read.csv('OTC.IM.Plasma.20.txt', sep = "\t", comment.char = '#')

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.