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

  • SEARCH
  • Home
  • 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 7947263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:22:57+00:00 2026-06-04T01:22:57+00:00

I am writing a function which would get all files from a particular directory

  • 0

I am writing a function which would get all files from a particular directory and then execute some commands on those files that match my criteria. One of the criteria is that the file should not have the name “test.csv”, and the other that it shouldn’t be a png-file. Now, the thing with test.csv works fine, but I don’t get R to ignore png-files, so unfortunately my script does not work as long as there are any png-files in the working directory. Here is an extract from my function:

file_list = list.files()

for (file in file_list){
  if ((file != "test.csv") & (grep(".*png", file) != 1)){
    my.file <- read.table(file)
  }
}

I’ve tried all kinds of variations of the grep command like grep(".*png", file) == 0, length(grep(".*png", file)) < 1 and similar things, but nothing worked. Any ideas what’s going wrong?

  • 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-04T01:22:59+00:00Added an answer on June 4, 2026 at 1:22 am

    How about using grepl, which returns a logical as to whether the pattern appears in the string in question:

    !grepl(".*png", file)
    

    I’d also change the regular expression to specifically look for “.png” at the end of the file name, using ".*\\.png$"

    So your code could be changed to

    for (file in file_list){
    #  if ((file != "test.csv") & !grepl(".*\\.png$", file)){
    # Actually, you could move both tests into one call to grepl:
      if(!grepl("test.csv|.*\\.png$", file)){
        my.file <- read.table(file)
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing this function which I want to print all the sublists of a
I am writing a function in which I need to read a string contains
I'm writing a basic function in PHP which takes an input string, converts a
I am writing a Function that has a return-type which will dynamically change depending
I'm writing a function intended to sanitize a poorly formatted string which the php
I am writing a functor F which takes function of type void (*func)(T) and
I am writing down a function and it takes an argument which is the
I'm writing a jQuery plugin , which similar to this, $(this).each(function(){ $el = $(this).find('.el')
I am writing a library which uses a few functions from the windows user32.dll
Writing an application for a custom gallery, and all the script files are 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.