0

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          
  • 1
    Welcome on SO. Please read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and use `dput` for your example data. – sgibb Aug 28 '13 at 07:44

1 Answers1

1

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))
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • @ Didzis Elferts: Sorry for not mentioning earlier, I would like to calculate the correlation between columns of the two dataframe and that was why I transposed what I had originally. – user1834063 Aug 28 '13 at 08:02
  • @user1834063 You can't calculate correlation if you have only one observation! – Didzis Elferts Aug 28 '13 at 08:05