Naveen Khattar
Naveen Khattar

Reputation: 21

How to export results from 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

Upvotes: 2

Views: 1537

Answers (3)

Daniel James
Daniel James

Reputation: 1433

Use a very simple copy and paste method to copy results from R console and paste to excel file. It helps you to correctly copy your data frame to your computer clipboard such that you can past it to wherever you like. Follow this code:

write_excel <- function(x, row.names = TRUE, col.names = TRUE) {
  write.table(x, "clipboard", sep = "\t", row.names = row.names, col.names = col.names) # this is the function that do the `copy` margic.
}

library(magrittr)
Faltu %>% 
  group_by(subgroup) %>%
  do(tidy(lm(sales ~ month_year + weekday + sequence, .))) %>%
  write_excel() # This is the function call

with this go to the excel file your data frame to seat and do Ctrl + C on windows. or right-click and choose paste.

Watch the video well demonstrated on YouTube here https://www.youtube.com/watch?v=I8vvw8V5aSc&t=143s You will get the gist starting from 7:44 minutes of the video, please watch to the end.

Please subscribe, like and ring the notification bell to this channel

Upvotes: 1

Valery Mukhin
Valery Mukhin

Reputation: 11

I just copy results to the clipboard and paste on the Excel sheet. Then I use the splitting on the columns with the Excel Data -> Text to columns wizard. Then you can save the sheet as csv.

Upvotes: 0

TarJae
TarJae

Reputation: 79311

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

Upvotes: 2

Related Questions