Reputation: 3
I have a data frame that is 68 x 252. I am trying to find the log change between every vector relative to the first vector. ie if v1 = 68 and v2,v3,v4 = 70, 71, 72... I want the new data frame output to be 0, 0.013,0.019,0.025.
data<-data[3:nrow(data)]/data[2:2]
returns
"Error in Ops.data.frame(data[3:nrow(data)], data[2:2]) : ‘/’ only defined for equally-sized data frames"
I have also tried making each their own matrix
div<-as.vector(data[2:2])
num<-as.matrix(data[3:nrow(data)])
test<-num/div
returns:
Error in num/div : non-conformable arrays
or
log(num/div)
Upvotes: 0
Views: 71
Reputation: 297
I think data.frames are easier to work with than matrixes. I like to keep each step of the problem as a different data.frame so it is easy to retrace a step if I need to and I can see clearly what each transformation has done.
If we start with some sample data (although typically you would be reading this in from a file using read.csv
or something similar):
#get data as a data frame
df <- structure(list(v1 = c(68, 120), v2 = c(70, 121), v3 = c(71, 122), v4 = c(72, 123)), class = "data.frame", row.names = c("row1", "row2"))
df
v1 v2 v3 v4
row1 68 70 71 72
row2 120 121 122 123
Convert to log(base10):
#take log (base 10) of data frame
df_log <- log10(df)
df_log
v1 v2 v3 v4
row1 1.832509 1.845098 1.851258 1.857332
row2 2.079181 2.082785 2.086360 2.089905
Last step...
df_ans <- df_log[,] - df_log[,1]
which means, from every row and column of our log data frame df_log[,]
subtract the value from the first column df_log[,1]
and store the results in df_ans
.
Result:
df_ans
v1 v2 v3 v4
row1 0 0.012589127 0.018749436 0.02482358
row2 0 0.003604124 0.007178585 0.01072387
Note: this last operation must be done with two different data frames df_ans
and df_log
. If you tried to do it all on the same data.frame like df_log <- df_log[,] - df_log[,1]
, it won't work as df_log[,1]
will be modified to 0
mid-operation and then you would be subtracting 0
from all cells which will not work very well!
Upvotes: 0