Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export results from R?

Tags:

r

I am using the below function to run multiple regression models, need to understand how to export the results to a .csv file so that I can use it in my database.

Faltu %>% 
  group_by(subgroup) %>%
  do(tidy(lm(sales ~ month_year + weekday + sequence, .)))

Where Faltu is my data frame.

Data in the image needs to be exported

like image 553
Naveen Khattar Avatar asked Jan 20 '26 22:01

Naveen Khattar


1 Answers

We could pipe write.csv at the end of your code:

Documentation of write.csv see here: https://www.rdocumentation.org/packages/AlphaPart/versions/0.8.1/topics/write.csv

# general:
write.csv(df,'Result.csv', row.names = FALSE)

# with your code:

Faltu %>% 
  group_by(subgroup) %>% 
  do(tidy(lm(sales ~ month_year + weekday + sequence, .))) %>% 
  write.csv('Result.csv')

Output: file: Result.csv in your working directory:

"subgroup","term","estimate","std.error","statistic","p.value"
1,"Mens","Fixed","(Intercept)","0.849",0.302
2,"Mens","Fixed","month_year","0.0113",0.0226
3,"Mens","Fixed","weekday","0.449",0.0425
4,"Mens","Fixed","sequence","-0.000914",0.000322
5,"Mens","Pants","(Intercept)","0.474",4.63
6,"Mens","Pants","month_year","0.112",0.233
7,"Mens","Pants","weekday","0.387",0.362
8,"Mens","Pants","sequence","-0.204",0.342
9,"Mens","Pull","On","(Intercept)",0.271
10,"Mens","Pull","On","month_year",0.0514
like image 169
TarJae Avatar answered Jan 22 '26 10:01

TarJae