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 8466109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:14:43+00:00 2026-06-10T15:14:43+00:00

I need to access variables from two different data sets in a simple loop

  • 0

I need to access variables from two different data sets in a simple loop (below). (I realize this requires the Negative and Positive vectors to be the same length…luckily, this will always be the case.)

Groups<-c("bdiControl","bdi")
Positive<-c("PA","Sad_PA","Amuse_PA","Happy_PA","Disgust_PA")
Negative<-c("NA","Sad_NA","Amuse_NA","Happy_NA","Disgust_NA")

for (g in Groups) {
    for (i in Positive) { 

if (sd(Groups[[g]]$Positive[[i]])<sdthresh | sd(Groups[[g]]$Negative[[i]]])<sdthresh){
cat('Standard deviation too low to run\ ',Positive[[i]],Negative[[i]],'\ comparison')
}
else{
corr<-cor(Groups[[g]]$Positive[[i]],Groups[[g]]$Negative[[i]],use="complete.obs") 
print("The correlation between " Positive[[i]] " and " Negative[[i]] " was " corr "for " Groups[[g]])
}
}
}

Other references I have tried include g$i, Groups[g]$Positive[i], g$Positive[[i]], and similar permutations. I think I am spinning my wheels in a rut of problem solving. Help?! 🙂

  • 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-10T15:14:45+00:00Added an answer on June 10, 2026 at 3:14 pm

    There were many issues with this code. While it wasn’t entirely clear what the code was trying to do (you should ask your question more clearly), I believe this will do what you want:

    for (group.name in Groups) {
        g <- get(group.name)  # retrieve the actual data
        for (i in 1:length(Positive)) { 
            if (sd(g[[Positive[i]]]) < sdthresh | sd(g[[Negative[i]]]) < sdthresh) {
                   cat('Standard deviation too low to run\ ',
                        Positive[[i]], Negative[[i]], '\ comparison')
            }
            else{
                corr<-cor(g[[Positive[i]]], g[[Negative[i]]],use="complete.obs")
                print(paste("The correlation between", Positive[[i]],
                        "and", Negative[[i]], "was", corr, "in", group.name))
            }
        }
    }
    

    For example, when I created random datasets (always provide a reproducible example!) with:

    set.seed(1)
    bdicontrol = as.data.frame(matrix(rnorm(100), nrow=10))
    bdi = as.data.frame(matrix(rnorm(100), nrow=10))
    colnames(bdicontrol) <- c(Positive, Negative)
    colnames(bdi) <- c(Positive, Negative)
    

    The output is:

    [1] "The correlation between PA and NA was -0.613362711250911 in bdicontrol"
    [1] "The correlation between Sad_PA and Sad_NA was 0.321335485805636 in bdicontrol"
    [1] "The correlation between Amuse_PA and Amuse_NA was 0.0824438791207575 in bdicontrol"
    [1] "The correlation between Happy_PA and Happy_NA was -0.192023690189678 in bdicontrol"
    [1] "The correlation between Disgust_PA and Disgust_NA was -0.326390681138363 in bdicontrol"
    [1] "The correlation between PA and NA was 0.279863504447769 in bdi"
    [1] "The correlation between Sad_PA and Sad_NA was 0.115897422274498 in bdi"
    [1] "The correlation between Amuse_PA and Amuse_NA was -0.465274556165398 in bdi"
    [1] "The correlation between Happy_PA and Happy_NA was 0.268076939911701 in bdi"
    [1] "The correlation between Disgust_PA and Disgust_NA was 0.573745174454954 in bdi"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my MainViewController implementation, I need to access variables from two different classes. one
I have two different modules that need access to a single file (One will
I need to access variables in whole script that is divided to few sections
Pretty often I need to access $config variables in views. I know I can
I need access to the uint64_t typedef from stdint.h in some wrapper code that
I know that's bad design, but I need access to the view from my
I need to access a components tag attribute like: <h:inputtext id=input_age/> from a backing
I need to access the commandline from within a C# application. I can't find
i want to make a notification function, and i need fields from 2 different
I have a function f(x,y) of two variables, of which I need to know

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.