Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:06:04+00:00 2026-06-07T02:06:04+00:00

I’ve written a script based on a for-loop to read in columns of multiple

  • 0

I’ve written a script based on a for-loop to read in columns of multiple .xls files, combine them to a single data frame, search for negative values and write a .txt file with these values and the name of the file.
The script works basically, but I have several hundred files to process, and it’s a bit slow. This version of the script is only a basic framework for later statistical analysis, and I want to parallelize the execution to speed it up.
I’ve tried to avoid the for-loop by applying the function via lapply and the plyr-package, but had problems passing the file list to “readWorkSheetFromFile” (Error in path.expand (filename) : invalid ‘path’ argument).

Here is the working script:

require(XLConnect)
setwd(choose.dir())

input = list.files(pattern = ".xls$")

# creates empty data frame 
df = data.frame(Name=NULL, PCr=NULL, bATP=NULL, Pi=NULL)

for(i in seq(along=input)){
    data = data.frame(readWorksheetFromFile(input[i], sheet="Output Data", 
    startRow=2, startCol=c(10, 13, 16), endCol=c(10, 13, 16), header=TRUE))

    head(data, n = -1L)

    colnames(data) = c("PCr", "bATP", "Pi")
    data$Name = file.path(input[i])

    attach(data)
    df = rbind(data, df)
    attach(df)
    rm(data)
}

# searches for negative values in df and writes to txt file 
neg_val = subset(df, bATP<0 | Pi<0 | PCr<0)
write.table(neg_val, file = "neg_val.txt", sep = "\t", quote=F)

Any solutions to this problem, or other suggestions to speed up execution?

Thanks,
Markus

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:06:06+00:00Added an answer on June 7, 2026 at 2:06 am

    I still don’t know why Martins code is not working on my data, but I’ve found another solution. It was about 4x faster in a first test than my original approach.

    # load required packages
    require(XLConnect)
    # set working dir
    setwd(choose.dir())
    
    # creates list of files of chosen dir and all subdirectories
    files = list.files(pattern = ".xls$", recursive=T, full.names=T)
    
    data = do.call("rbind", lapply(files, function(fl) {
       # Read data from file
       data.tmp = data.frame(readWorksheetFromFile(file = fl, sheet="Output Data", 
                             startRow=2, startCol=c(10, 13, 16), 
                             endCol=c(10, 13, 16), header=TRUE))
    
      # deletes last row of data frame
      head(data.tmp, n = -1L)
    
      # add file names as column 
      data.tmp$File = file.path(fl)
      data.tmp
    }))
    
    # rename columns
    colnames(data) = c("PCr", "bATP", "Pi", "File")
    # list negative values 
    neg.val = subset(data, bATP<0 | Pi<0 | PCr<0)
    # write output file
    write.table(neg.val, file = "neg_val.txt", sep = "\t", quote=F)
    

    Thanks to all and best regards,
    Markus

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.