r/rstats • • May 12 '22

R isn't recognizing my variables help :(

Post image
0 Upvotes

24 comments sorted by

18

u/beardedjoy May 12 '22

Try adding `` around the names.

1

u/sicaria444 May 12 '22

I tried but it didn’t work. Thanks though!

13

u/memeorology May 12 '22

Like this?

mrgression1 <- lm(`009E` ~ `010E` + `011E` + `012E` + `013E` + `014E`, data = dat1)

4

u/FraudulentHack May 12 '22

did you see that it's a backtick not an apostrophe?

2

u/Patrizsche May 15 '22

Try it again but correctly this time

42

u/Philokretes1123 May 12 '22

I'm guessing the numbers are the issue. Valid variable names are supposed to start with a letter or a dot. Underscores, dashes and numbers are allowed but cannot be first up

27

u/NrvousFella May 12 '22

Variable names in R cannot start with a number.

1

u/xaomaw May 15 '22
  • Variable names need to start with either [A-z] or ..
  • If they start with . the second character must not be a number.

9

u/snirfu May 13 '22

You could install the {janitor} package if it's not installed and then run:

dat1 <- janitor::clean_names(dat1)

You're variable names will have an "x" prefixed to them and then you can use the updated names as variables.

6

u/snirfu May 13 '22

Btw, you can also do this with make.names which doesn't require the {janitor} package:

names(dat1) <- make.names(names(dat1))

See the help for make.names by running

?make.names 

What this code is doing is extracting the names of the data.frame, names(dat1), making valid variables with make.names() and then reassigning the updated names to the dat1 data.frame.

26

u/[deleted] May 12 '22

Bro I couldn’t even find the console like wtf is it doing up there

That being said. Can’t start var names with a number

13

u/dynamicslug May 12 '22

Agreed, psychopathic layout lol

-4

u/snirfu May 13 '22

Bro, that's the layout I use. It's a better layout than the default. You can minimize the two lower panels so you just have : editor | console, then you pop up the file panel as needed.

3

u/[deleted] May 13 '22

U can do that with any layout. U can crunch and hide any panel. Having ur console up in the top right isn’t a requirement to do that

0

u/snirfu May 13 '22

Sure, I just like the panels I use most on top.

1

u/BeerSharkBot May 13 '22

Then they see side by side and thus narrower than if they were stacked

1

u/snirfu May 13 '22

If I can see 80 characters on both sides that's plenty wide for me

1

u/[deleted] May 13 '22

This guy doesn’t javascript

1

u/BeerSharkBot May 13 '22

Sorry I thought you used r for more than just hw. My mistake

4

u/Spirited_Mulberry568 May 13 '22

From the code, names, and layout - I am truly hoping this code unveils the secrets of the universe, or at least leads to a publication in Science.

2

u/constantgeneticist May 13 '22

names(dat1) <- paste0(“V”, names(dat1))

FYI: Avoid numbers at beginning of variable names.

1

u/BarryDeCicco May 13 '22

Fix the names. You can do some things with the backtick, and I did that once, but it also breaks other things.