Jabernet
Jabernet

Reputation: 431

Problems with read data in R

I usually enter data into R by hand, but I have this data set with n=27 elements. I read the data with data = read.table("HW6.txt", header = TRUE) The working environment shows the data there, X=23,19,... and Y=27.1,22.1,...

I can get my least squares just fine:

results=lm(Y~X, data) and 
lm(formula = Y ~ X, data = data)

# Coefficients: (Intercept) X
# 1.800 1.004

But for whatever reason I make calls to X or Y to do further manipulation, i.e.

sum(X) 
# Error: object 'X' not found, 

or

mean(X) 
# Error in mean(X) : object 'X' not found

So, is there something else I have to do to get it to work properly?

Upvotes: 1

Views: 159

Answers (2)

Amrita Sawant
Amrita Sawant

Reputation: 10913

Individual elements can be accessed as below

data = read.table("HW6.txt", header = TRUE)
data$X
sum(data$X)

Upvotes: 7

Thomas
Thomas

Reputation: 44555

When you enter data "by hand" into R, you're creating variables in the .GlobalEnv, i.e. the workspace that you encounter when you open R.

When you instead load data into R using read.table, etc. you are creating a data.frame where those variables are stored. As @Amrita's answer shows you, you can then extract variables from that data.frame using [ or $ extraction.

Thus an option for you (i.e., the option that will create behavior most similar to your previous workflow) is the following:

d <- read.table("HW6.txt")
X <- d$X
Y <- d$Y
rm(d)

Then you can just refer to the variables as X and Y without having to do any of the things I described earlier.

But! It's probably just better to correct your workflow and start using $ extraction from a data.frame rather than fall back on the convenience of what you've been doing previously.

(Note 1: In an interactive session, you can also use with(data, ...) to execute expressions using objects from your data.frame without using $ or [ extraction. It's generally discouraged to use this in a script/package/etc.)

(Note 2: To recreate the behavior you're used to, you can use attach(data), which will add the data.frame to R's search path. This is discouraged, though, because it can have unintended consequences. Among these, if you modify X in the .GlobalEnv, it does not modify it in the original data.frame, thus creating confusion later one if you try to call either object.)

Upvotes: 3

Related Questions