Reputation: 262
I am processing list data data that I am doing matrix calculations on and I am getting some strange behavior.
When I pull the data from the list and apply the tcrossprod function I get different answers than I would expect. Below is a simplified example of the issue.
My data is of this form...
> test_list <- list(data.frame("col1" = c('a','b'), "col2" = c(229187.82,135323.01)))
> test_list
[[1]]
col1 col2
1 a 229188
2 b 135323
Applying tcrossprod gives an incorrect result on the data...
> tcrossprod(test_list[[1]]$col2)
[,1] [,2]
[1,] 52527056836 31014385658
[2,] 31014385658 18312317035
This is the correct answer...
> vec <- c(229188,135323)
> vec
[1] 229188 135323
> tcrossprod(vec)
[,1] [,2]
[1,] 52527139344 31014407724
[2,] 31014407724 18312314329
And here is the difference between the two calculations...
> tcrossprod(vec) - tcrossprod(test_list[[1]]$col2)
[,1] [,2]
[1,] 82507.6 22066.26
[2,] 22066.3 -2706.46
I can't find a reason why there is a discrepancy between the two calculations...
> typeof(test_list[[1]]$col2)
[1] "double"
> typeof(vec)
[1] "double"
> class(test_list[[1]]$col2)
[1] "numeric"
> class(vec)
[1] "numeric"
Is there a way to fix this issue so the numbers calculated are correct?
Upvotes: 0
Views: 38
Reputation: 171
It is because the real value in your dataframe is c(229187.82,135323.01). Then when you display they round it to: c(229188,135323)
In your example you then use the rounded numbers which are not the right ones.
> test_list <- list(data.frame("col1" = c('a','b'), "col2" = c(229187.82,135323.01)))
> a<-test_list[[1]]$col2
> vec1 <- matrix(c(229187.82,135323.01),nrow=2)
> tcrossprod(vec1)
[,1] [,2]
[1,] 52527056836 31014385658
[2,] 31014385658 18312317035
> vec2=matrix(a,nrow=2)
> tcrossprod(vec2)
[,1] [,2]
[1,] 52527056836 31014385658
[2,] 31014385658 18312317035
Upvotes: 2