0

I am trying to create and name 26 variables( vectors). I can do the operation manually like this : a=c() ; b=c();c=c() ...z=c() . However ,that's not the most clever way to solve the issue. So, let's suppose that we have a list composed of {'a','b,'c',...'z'} and i want to create the variables a,b,c...z (empty vectors) . I'm thinking for a iterative loop :

for (variable in letters[1:26]){

variable=c()

}

I know that doing it this way won't work, that's why i couldn't figure out another way to solve it.

SOLVED: I got an answer for my main issue.

Moni93
  • 25
  • 7
  • 3
    It's possible to do. But don't do it. Why do you want to do this? I guarantee there are better ways to accomplish your *actual* goal. – Dason Feb 14 '18 at 15:32
  • This doesn't sound like a very R-like way to do things. This question is more like a case of the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Maybe think about using named lists. They can often act as environments where there names can be treated like variables. – MrFlick Feb 14 '18 at 15:38
  • I am doing a linear regression and i have a categorical varible name X: it takes 26 possible values( strings). The issue is that i am not allowed to use that variable for my linear regression and i need to split it to 26 variables. for exemple if for a certain row n, X="JAVA", then one of the 26 variables will be called JAVA an will take value=1 and all the others will be equal to zero. The idea is that the 26 variables have the same name as the 26 values in X. – Moni93 Feb 14 '18 at 15:38
  • 2
    So you're trying to create binary (dummy) variables. If your column is already a factor, many functions like lm / glm will do this for you. – talat Feb 14 '18 at 15:40
  • 1
    As docendo says, many modeling functions take care of this for you. They use `model.matrix` to do it - so if you need to do it yourself you can also use `model.matrix`. If you search this site for "[r] create dummy variables" or "[r] one hot encoding" (another name for this), you will find many examples. – Gregor Thomas Feb 14 '18 at 15:43
  • @Gregor :Thank you. that's what i was looking for ... – Moni93 Feb 14 '18 at 16:09
  • @Gregor pls convert your comment to an answer, or mark the question as a duplicate, so that this question moves off the unanswered questions list. – dcorking Feb 14 '18 at 16:24

0 Answers0