Readxl Question

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Readxl Question

Thomas Subia-2
Colleagues,

I am using readxl to extract a serial number and its associated data using the following code.

library(readxl)
files <- list.files(pattern="*.xls", full.names = FALSE)
serials <- lapply(files, read_excel, sheet="Flow Data", range=("c6"))
flow.datum <- lapply(files, read_excel, sheet="Flow Data", range=("c22:c70"))
dates <- lapply(files, read_excel, sheet="Flow Data", range=("h14"))

Here each serial and date is associated with 49 data points in flow.datum.

Now I write the serials, flow data and dates into a text file using:

datesdf <- as.data.frame(dates)
mydates <- list(datesdf)
write.table(mydates,"dates.txt",sep="\t")

serialdf <- as.data.frame(serials)
myserials <- list(serialdf)
write.table(myserials,"serials.txt",sep="\t")

flowdf <-as.data.frame(flow.datum)
myflow <- list(flowdf)
write.table(myflow,"myflow.txt",sep=",")

The problem with the dates.txt and the serials.txt is that they need to associated with its 49 corresponding values in myflow.txt.

The objective is to create a text file having this format:

Serial Date Flow

I'm not sure how to do this. Any suggestions would be appreciated.

Thomas Subia
Statistician / Senior Quality Engineer
ASQ CQE

IMG Companies 
225 Mountain Vista Parkway
Livermore, CA 94551
T. (925) 273-1106
F. (925) 273-1111
E. [hidden email]


Precision Manufacturing for Emerging Technologies
imgprecision.com 

The contents of this message, together with any attachments, are intended only for the use of the individual or entity to which they are addressed and may contain information that is legally privileged, confidential and exempt from disclosure. If you are not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this message, or any attachment, is strictly prohibited. If you have received this message in error, please notify the original sender or IMG Companies, LLC at Tel: 925-273-1100 immediately by telephone or by return E-mail and delete this message, along with any attachments, from your computer. Thank you.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Readxl Question

Jim Lemon-4
Hi Thomas,
Perhaps this is what you are seeking:

my_read_excel<-function(filename) {
 serials<-read_excel(filename,sheet="Flow Data",range=("c6"))
 flow.data<-read_excel(filename,sheet="Flow Data",range=("c22:c70"))
 dates<-read_excel(filename,sheet="Flow Data",range=("h14"))
 return(data.frame(Serial=rep(serials,49),Date=rep(dates,49),
  Flow=flow.data),stringsAsFactors=FALSE)
}
lapply(files,my_read_excel)

Should return a list of data frames in the format you want. If you
want just one big data frame, rbind the results. You should also be
able to convert the dates from character to date type without striking
the factor problem.

Jim

On Sat, Dec 21, 2019 at 4:16 AM Thomas Subia <[hidden email]> wrote:
>
> Colleagues,
>
> The objective is to create a text file having this format:
>
> Serial  Date    Flow
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.