user1834063
user1834063

Reputation: 41

NA s in Correlation in R

I have two dataframes with a single row and would like to find the correlation using cor() function in R.

### data A

 structure(list(`244901_at` = 5.9926850249, `244902_at` = 6.3553842023, 
`244903_at` = 8.8921318402, `244904_at` = 6.4579518676, `244905_at` = 4.7964593532, 
`244906_at` = 8.3237756365, `244907_at` = 4.3723366423, `244908_at` = 4.7352416175, 
`244909_at` = 4.5714368032, `244910_s_at` = 4.1291856864), .Names = c("244901_at", 
 "244902_at", "244903_at", "244904_at", "244905_at", "244906_at", 
"244907_at", "244908_at", "244909_at", "244910_s_at"), class = "data.frame", row.names =  c(NA, -1L))
data B
     structure(list(`244901_at` = 4.750238726, `244902_at` = 5.0413815841, 
`244903_at` = 4.9859823666, `244904_at` = 6.1587895393, `244905_at` = 4.8531009472, 
`244906_at` = 5.6846558629, `244907_at` = 4.584193219, `244908_at` = 4.5031021576, 
`244909_at` = 4.4333119965, `244910_s_at` = 4.1019972842), .Names = c("244901_at", 
 "244902_at", "244903_at", "244904_at", "244905_at", "244906_at", 
 "244907_at", "244908_at", "244909_at", "244910_s_at"), class = "data.frame", row.names       = c(NA, -1L))

when I calculate the correlation it gives me NA.

     cor(data A, data B)



                           244901_at 244902_at 244903_at 244904_at 244905_at 244906_at   244907_at               2    44908_at
             244901_at          NA        NA        NA        NA        NA        NA        NA        NA
             244902_at          NA        NA        NA        NA        NA        NA        NA        NA
             244903_at          NA        NA        NA        NA        NA        NA        NA        NA
             244904_at          NA        NA        NA        NA        NA        NA        NA        NA
             244905_at          NA        NA        NA        NA        NA        NA        NA        NA
             244906_at          NA        NA        NA        NA        NA        NA        NA        NA
             244907_at          NA        NA        NA        NA        NA        NA        NA        NA
             244908_at          NA        NA        NA        NA        NA        NA        NA        NA
             244909_at          NA        NA        NA        NA        NA        NA        NA        NA
             244910_s_at        NA        NA        NA        NA        NA        NA        NA        NA  

                        244909_at 
           244901_at          NA          
           244902_at          NA          
           244903_at          NA          
           244904_at          NA          
           244905_at          NA          
           244906_at          NA          
           244907_at          NA          
           244908_at          NA          
           244909_at          NA          
           244910_s_at        NA          

Upvotes: 0

Views: 786

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98599

If your data are in data frame then function cor() will calculate correlation between columns of your two data frame. In your case you get all NA because there is only one row in your data frame.

You have to transpose your data frames so that this one row becomes one column and then you can calculate correlation coefficient. To transpose you can use function t().

cor(t(df.A),t(df.B))

Upvotes: 1

Related Questions