0

I have a few problems concerning the same topic.

(1) I am trying to loop over:

premium1999 <- as.data.frame(coef(summary(data1999_mod))[c(19:44), 1])

for 10 years, in which I wrote:

for (year in seq(1999,2008)) { 
    paste0('premium',year) <- as.data.frame(coef(summary(paste0('data',year,'_mod')))[c(19:44), 1])
}

Note: for data1999_mod is regression results that I want extract some of its estimators as a dataframe vector. The coef(summary(data1999_mod)) looks like this:

#A matrix: ... of type dbl
             Estimate        Std. Error     t value    Pr(>|t|)
    age      0.0388573570  2.196772e-03   17.6883885  3.362887e-6
    age_sqr -0.0003065876  2.790296e-05  -10.9876373  5.826926e-28
    relation 0.0724525759  9.168118e-03    7.9026659  2.950318e-15
    sex     -0.1348453659  8.970138e-03  -15.0326966  1.201003e-50
    marital  0.0782049161  8.928773e-03    8.7587533  2.217825e-18
    reg      0.1691004469  1.132230e-02   14.9351735  5.082589e-50 
    ...

However, it returns Error: $ operator is invalid for atomic vectors, even if I did not use $ operator here.

(2) Also, I want to create a column 'year' containing repeated values of the associated year and am trying to loop over this:

premium1999$year <- 1999

In which I wrote:

for (i in seq(1999,2008)) {
    assign(paste0('premium',i)[['year']], i) 
}

In this case, it returns Error in paste0("premium", i)[["year"]]: subscript out of bounds

(3) Moreover, I'd like to repeat some rows and loop over:

premium1999 <- rbind(premium1999, premium1999[rep(1, 2),])

for 10 years again and I wrote:

for (year in seq(1999,2008)) {
    paste0('premium',year) <- rbind(paste0('premium',year), paste0('premium',year)[rep(1, 2),])
}

This time it returns Error in paste0("premium", year)[rep(1, 2), ]: incorrect number of dimensions

I also tried to loop over a few other similar things but I always get Error.

Each code works fine individually.

I could not find what I did wrong. Any help or suggestions would be very highly appreciated.

2
  • For your issue 1, try this: for (year in seq(1999,2008)) { assign (paste0('premium',year), <whatever_value_you need>) } It seems to me you are using unnecessary loops. If you specify sample data input and expected output (no screen shots please), it will be easier to help. Commented May 14, 2020 at 16:37
  • I tried but it still returns the same error. I will edit more info about concerning (1) problem on the thread. I want to extract the regression estimators from the "data1999_mod" for rows 19 to 44 as a data frame. Commented May 14, 2020 at 16:47

1 Answer 1

1

The problem with the code is that the paste0() function returns the character and not calling the object that is having the name as this character. For example, paste0('data',year,'_mod') returns a character vector of length 1, i.e., "data1999_mod" and not calling the object data1999_mod.

For easy understanding, there is huge a difference between, "data1999_mod"["Estimate"] and data1999_mod["Estimate"]. Subsetting as data frame merely by paste0() function returns the former, however, the expected output will be given by the latter only. That is why you are getting, Error: $ operator is invalid for atomic vectors.

The same error is found in all of your codes. On order to call the object by the output of a paste0() function, we need to enclose is by get().

As, you have not supplied the reproducible sample, I couldn't test it. However, you can try running these.

#(1)

for (year in seq(1999,2008)) { 
  paste0('premium',year) <- as.data.frame(coef(summary(get(paste0('data',year,'_mod'))))[c(19:44), 1])
}


#(2)

for (i in seq(1999,2008)) {
  assign(get(paste0('premium',i))[['year']], i) 
}


#(3)

for (year in seq(1999,2008)) {
  paste0('premium',year) <- rbind(get(paste0('premium',year)), get(paste0('premium',year))[rep(1, 2),])
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. I tried the code you gave me but the first and third one still returns error <target of assignment expands to non-language object> and the second one returns Error <invalid first argument>. I actually tried using get() before too, but none of it worked..
Can you update a minimal reproducible sample, so that I could run the code and edit my answer?
Hi. thank you very much. I think I know what the problem was. The code get() works after all. However, the left side still also returns "premium1999" because of paste0(). So I have created a list to store each dataframe so that it can be called by, for example, premium[["premium1999"]] instead. By doing this, I can use paste0() inside the parentheses, and it works. Thank you very much for your explanation about the paste0() code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.