user3385769
user3385769

Reputation: 161

basic matrix/data.frame arithmetic in R

I am finding it puzzling that I am not able to do simple matrix arithmetic in R. Is this really not possible? I want to take the values in 1 dataframe and subtrack them row all of the columns in another dataframe. Seems like this should be easy but I am not finding anything, although there is a "psych" package which as a function but doesn't give the expected results. Also seems like it should not be necessary. Any tips. Much appreciated.

> NROW(arData1[c(gsub(" ", ".",portfolio1), "benchmark")])
[1] 51
> NCOL(arData1[c(gsub(" ", ".",portfolio1), "benchmark")])
[1] 4
> NROW(arData1[gsub(" ", ".", treasuries)])
[1] 51
> NCOL(arData1[gsub(" ", ".", treasuries)])
[1] 1

ExRet1 <- arData1[c(gsub(" ", ".",portfolio1), "benchmark")] - arData1[gsub(" ", ".", treasuries)]
Error in Ops.data.frame(arData1[c(gsub(" ", ".", portfolio1), "benchmark")],  : 
  - only defined for equally-sized data frames

Upvotes: 1

Views: 9376

Answers (1)

jlhoward
jlhoward

Reputation: 59425

The problem has to do with the way you are indexing.

df["colname"]

extracts the column colname as a data frame. If you subtract two data frames, R does this column-wise and row-wise, so both data frames must have the same number of columns.

However, you can extract colname as a vector using [[, as in

df[["colname"]]

If you subtract a vector from a data frame, that vector is subtracted from each column in df.

df <- data.frame(x=1:5,y=11:15,z=21:25,A=31:35)
df[c("x","y","z")] - df["A"]
# Error in Ops.data.frame(df[c("x", "y", "z")], df["A"]) : 
#   - only defined for equally-sized data frames

df[c("x","y","z")] - df[["A"]]
#     x   y   z
# 1 -30 -20 -10
# 2 -30 -20 -10
# 3 -30 -20 -10
# 4 -30 -20 -10
# 5 -30 -20 -10

Upvotes: 5

Related Questions