suemi
suemi

Reputation: 69

is the vector able to %*%matrix or dataframe ?inR

Consider for code given :

ne<-rep(1,n)
meanx <- drop(one %*% x)/n

then there is an error reported

is the vector able to %*%matrix or data.frame ?

is this only caused by version of R?

Upvotes: 0

Views: 118

Answers (2)

David Marx
David Marx

Reputation: 8558

The %*% operator won't work on a dataframe, but if you cast the data.frame to a matrix it will work.

X = rnorm(100)
Y = rnorm(100)
df = data.frame(X,Y)
M = as.matrix(cbind(X,Y))

# this works fine
X %*% M 

            X        Y
[1,] 99.95776 3.955938

# This one throws an error. But it can be fixed!
X %*% df    

Error in X %*% df : requires numeric/complex matrix/vector arguments

# Ta da!
X %*% as.matrix(df)

            X        Y
[1,] 99.95776 3.955938

Upvotes: 1

Doctor Dan
Doctor Dan

Reputation: 771

If you meant

n <- 100
one<-rep(1,n)
meanx <- drop(one %*% one)/n    ,

then it works like a charm...

Upvotes: 1

Related Questions