1

If I have R data like:

> European204cad.prs.basic$coefficients
age_diabetes_diagnosis                    Sex                 SCOREZ 
            0.05807447             0.27131656             0.33191227 
                  PCA1                   PCA2 
           32.81616512           -21.6415698

How can I access SCOREZ programmatically?

I tried

> European204cad.prs.basic$coefficients$SCOREZ
Error in European204cad.prs.basic$coefficients$SCOREZ : 
  $ operator is invalid for atomic vectors

But as you can see, it's wrong

I've tried other potential splits (maybe "element access" is better?), like @, or [ but none work.

I don't know the correct search terms.

How can I access the value of 0.3319 programmatically?

con
  • 5,767
  • 8
  • 33
  • 62
  • 2
    Try `coefficients(European204cad.prs.basic)["SCOREZ"]` – Ritchie Sacramento Dec 07 '22 at 23:05
  • 1
    or perhaps `European204cad.prs.basic$coefficients["SCOREZ"]`, noting that since it is not a `data.frame` nor `list`, the `$`-accessor does not work. Being a named vector, `[`-indexing is preferred. – r2evans Dec 08 '22 at 00:41
  • 1
    @r2evans-GONAVYBEATARMY if you write your answer, I'll accept – con Dec 08 '22 at 15:48

1 Answers1

1

You should be able to use

European204cad.prs.basic$coefficients["SCOREZ"]

The $-accessor works with data.frames and lists, but the coefficients are a named-vector, e.g., c(a=1, b=2). For that format, you need the [-accessor only. (Informative reading about $, [, and [[: The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe).

r2evans
  • 141,215
  • 6
  • 77
  • 149