Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:25:51+00:00 2026-06-16T03:25:51+00:00

if xmpl is a list where each element has an integer age and a

  • 0

if xmpl is a list where each element has an integer age and a list data, where data contains three matrices of equal size, a to c

What is the best way to do

cor( xmpl[[:]]$data[[:]][c('a','b','c')],  xmpl[[:]]$age)

where the results would be 3 x length(a) array or list that reflects age correlated with each instance of each element of a (row 1), b (row 2), and c (row 3) across xmpl.


I am reading in matrices that represent the output of different pipelines. There are 3 of these per subject and a whole lot of subjects. Currently, I’ve built a list of subjects that has among other things a list of pipeline matrices.

The structure looks like:

 str(exmpl)
 $ :List of 4
  ..$ id       : int 5
  ..$ age      : num 10
  ..$ data     :List of 3
  .. ..$ a: num [1:10, 1:10] 0.782 1.113 3.988 0.253 4.118 ...
  .. ..$ b: num [1:10, 1:10] 5.25 5.31 5.28 5.43 5.13 ...
  .. ..$ c: num [1:10, 1:10] 1.19e-05 5.64e-03 7.65e-01 1.65e-03 4.50e-01 ...
  ..$ otherdata: chr "ignorefornow"
  #[...]

I want to correlate every element of a across all subjects with the age of subjects. Then do the same for b and c and put the results into a list.

I think I am approaching this in a way that is awkward for R. I’m interested in what the “R way” of storing and retrieving this data would be.

Data Structure and desired output http://dl.dropbox.com/u/56019781/linked/struct-2012-12-19.svg

library(plyr)

## example structure
xmpl.mat  <- function(){ matrix(runif(100),nrow=10) }
xmpl.list <- function(x){ list(  id=x, age=2*x, data=list(  a=x*xmpl.mat(), b=x+xmpl.mat(), c=xmpl.mat()^x   ), otherdata='ignorefornow' ) }
xmpl      <- lapply( 1:5, xmpl.list )

## extract
ages <- laply(xmpl,'[[','age')
data <- llply(xmpl,'[[','data')

# to get the cor for one set of matrices is easy enough
# though it would be nice to do: a <- xmpl[[:]]$data$a
x.a      <- sapply(data,'[[','a')
x.a.corr <- apply(x.a,1,cor,ages)

# ...

#xmpl.corr   <- list(x.a.corr,x.b.corr,x.c.corr)

# and by loop, not R like?
xmpl.corr<-list()
for (i in 1:length(names(data[[1]])) ){
  x <- sapply(data,'[[',i)
  xmpl.corr[[i]] <- apply(x,1,cor,ages)
}
names(xmpl.corr) <- names(data[[1]])

Final output:

str(xmpl.corr)
List of 3
 $ a: num [1:100] 0.712 -0.296 0.739 0.8 0.77 ...
 $ b: num [1:100] 0.98 0.997 0.974 0.983 0.992 ...
 $ c: num [1:100] -0.914 -0.399 -0.844 -0.339 -0.571 ..
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:25:52+00:00Added an answer on June 16, 2026 at 3:25 am

    Here’s a solution. It should be short enough.

    ages <- sapply(xmpl, "[[", "age")                      # extract ages
    data <- sapply(xmpl, function(x) unlist(x[["data"]]))  # combine all matrices
    corr <- apply(data, 1, cor, ages)                      # calculate correlations
    xmpl.corr <- split(corr, substr(names(corr), 1, 1))    # split the vector
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I imported matplotlib in the way like this: import matplotlib as mpl A error
Table that holds content information - content : +----------+-------------+-------------------+--------------------------------+ | (int) id | (int)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.