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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:11:29+00:00 2026-05-20T18:11:29+00:00

I’d like to ask you a question again if you have some time. I

  • 0

I’d like to ask you a question again if you have some time.

I present to you my usual df dataframe that I already used before in my previous quesitons as a converted, simplified version of my real df dataframe which would be too difficult to show here. However the main characteristics are still the same.

id <-c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3)
a <-c(3,1,3,3,1,3,3,3,3,1,3,2,1,2,1,3,3,2,1,1,1,3,1,3,3,3,2,1,1,3)
b <-c(3,2,1,1,1,1,1,1,1,1,1,2,1,3,2,1,1,1,2,1,3,1,2,2,1,3,3,2,3,2)
c <-c(1,3,2,3,2,1,2,3,3,2,2,3,1,2,3,3,3,1,1,2,3,3,1,2,2,3,2,2,3,2)
d <-c(3,3,3,1,3,2,2,1,2,3,2,2,2,1,3,1,2,2,3,2,3,2,3,2,1,1,1,1,1,2)
e <-c(2,3,1,2,1,2,3,3,1,1,2,1,1,3,3,2,1,1,3,3,2,2,3,3,3,2,3,2,1,4)
df <-data.frame(id,a,b,c,d,e)
df
df.list <- lapply(df[,2:6],function(x, id){ t(table(x, id, useNA = "ifany")) }, df$id)
df.list

You see, basically what I’ve created here is a collection of the total sum of occurrences of each different number appearing in columns ’a’ to ’e’ and groupped by the ids in column id at the same time.

In the next step I created a loop which looks like the follows:

for (i in names(df.list))
{
  df.list[i]
  assign( paste("var",i,sep=""),
          (matrix(matrix(unlist(df.list[i])),ncol=nlevels(factor(df[,i])),nrow=3))/10
        )
}

It divides each and every element of the list created before by 10. However, it is just the first half of my whole loop which I wanted to implement but at least it STILL works fine and properly, I have no problem with it, just send these codes into R here ->

vara
varb
varc
vard
vare

Now the more difficult part comes now when I’m trying to perform the “for (k in 1:3)” section. So, let’s try to send these lines again (and also the previous half part of the whole loop of course).

for (i in names(df.list))
{
  df.list[i]
  assign( paste("var",i,sep=""),
          (matrix(matrix(unlist(df.list[i])),ncol=nlevels(factor(df[,i])),nrow=3))/10
        )

  for (k in 1:3)
    assign( paste("var",i,k,sep="."),
            vari[k,]*5 
          )
}

My problem is at the vari[k,]*5 line. (In real I ought to sort out a matrix multiplication at this point.) The code does not recognize vari however I already defined i before. And I do not intend to use vara, varb, varc… etc. because I need this to be automated. The reason: I’m gonna have to refresh my real df dataframe on a regular basis hence the number of variables might change over time (I will not necessarily have variables only from a to e, rather a to f or a to y etc.

So I get the following error message:

Error in assign(paste("var", i, k, sep = "."), vari[k, ] * 5): object 'vari' not found

What do I miss/do wrong here? I just want to refer to another object I already created in the same loop but still can’t recognize. Is there a proper solution here?

Thanks very much

  • 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-05-20T18:11:30+00:00Added an answer on May 20, 2026 at 6:11 pm

    I think you can replace

    vari[k,]*5 
    

    with

    get( paste( "var", i, sep="" ) )*5 
    

    Do you really need to create varaibles in this way though? I worry about the namespace getting out of hand if your data set gets any larger. It might be better to just create a list object, or define your own environment with new.env and set the variables in this environment instead of the global one?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask a theoritical question about how some web sites work.
This is a question that I have been pondering for a long time ,
I would like to ask a question. Now i am training in iOS with
I would like to ask a question on theming in liferay. What would be
I'd like to ask your opinion on this. This is a theoretical question. I'm
I would like to ask a rather, general, high-level introductory kind of question regarding
I don't know if this is the right place to ask a question like
So, I'm not sure how to ask this question, as it seems like it
I know this is a weird question to ask, but I would like to
I know this sounds like a dumb question but I need to ask this.

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.