0

I have a num of columns in the R such as column_1, column_2column_30. If I wrote a regression based on the sum of all columns: how can I write it as a sum up? (not by outcome = column_1+...+column_30)

If I want to create new variables(columns):

new_column1 = column_1 * 1.1
new_column2 = column_2 * 1.2
new_column3 = column_1 * 1.3

and so on. Is there a simple way (maybe by loop) achieved in R?

Thanks.

miken32
  • 42,008
  • 16
  • 111
  • 154
Lernst
  • 13
  • 4
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 06 '18 at 19:40

1 Answers1

0

If I understand what you want to do, this code can help you.

df1 = data.frame(replicate(30,rnorm(40,20,5)))
colnames(df1) = paste0("column_",1:30)
df1
y = 1:40
# attach(df1); lm(y ~ column_1 + column_2 + .... + column_30)
lm(y ~ ., data = cbind(y,df1))

df2 = matrix(NA,40,30)
for(i in 1:30){df2[,i] = df1[,i]*seq(1.1,4,0.1)[i]}
colnames(df2) = paste0("new_column_",1:30)
df = cbind(df1,df2)
df[1:5,c(1:4,56:60)]
       column_1 column_2 column_3  column_4 new_column_26 new_column_27 new_column_28 new_column_29 new_column_30
1 21.497199 12.40726 14.02247 22.410877      83.30869      81.12982      64.32517      36.74374      55.54547
2 19.976892 28.61573 26.81687  9.821782      86.66725      72.59295     104.11971      83.24529      91.68181
3  9.333362 15.95690 15.14520 11.023078      32.61563      57.83005      89.69093      68.25862      63.83636
4 16.442331 13.04171 15.54477 18.369973      85.30285     100.67551      95.59633      87.21039      89.42617
5 17.757539 19.24030 25.93277 20.191389      71.99046      68.09845      91.93291     105.11022      65.71628
Rafael Díaz
  • 2,134
  • 2
  • 16
  • 32