R: substract the Y column as a matrix

66 views Asked by At

I have a table called test (imported with read.csv) that looks like this:

Y   X1   X2   ...   X100
0   125   a   ...   32
1   163   b   ...   25
0   758   b   ...   587

I have succeeded saving all the predictors in a different table with the following command:

x_test <- test[, !(colnames(test) %in% c("Y"))]   

When I type

fix(x_test)

I get a beautiful table with all the predictors. I cannot seem to do the same for Y. Any help?

2

There are 2 answers

2
JasonD On BEST ANSWER

Hope this is helpful. Insufficient rep to ask for information in a comment, so I'll jump in with something. I don't know what class your loaded object is (or the columns therein), but you might try ensuring it's a data frame, then using subset:

#create some random nonsense
set.seed(123)
junk1 = data.frame(matrix(rnorm(100,5,.5),ncol=1,nrow=100))
junk2 = data.frame(matrix(1:100,ncol=100,nrow=100))
colnames(junk1)="Y"
test=cbind(junk1,junk2)

#should at this point have a data.frame something like your data, first column Y, rest X1..X100
#if yours isn't already, after doing your "test=read.csv(file)" then "test=as.data.frame(test)"

x_test = subset(test,select=colnames(test)[colnames(test)!="Y"])
y_test = subset(test,select="Y")

using the subset may not be the best way, but has the advantage that even when extracting a single column (as in the case of your "Y") the result will be a data frame, rather than a vector.

EDIT: props to Sven who I just saw enquired about the class (didn't see before posting this)

3
Sven Hohenstein On

I suppose you want

test["Y"]

instead of test[ , "Y"].