Use the R language to select a few columns of data for all TXT files in a folder

Source: Internet
Author: User
Tags sprintf

Sometimes we encounter such business needs, such as we have a lot of data files, there are many data items, a column represents a data item, and different file data items are arranged in a random order. But we only need to use a few columns of data, and we want to use a few columns of data in the given order, as shown below:

This problem in my blog useful python programming solution, here is mainly to solve with R, contrast the two languages to solve the problem of the shortcut, the following explanation is not in the details, can refer to the previous post, the following is the code:

InputDir  = "./raw"
OutputDir = "./select" for

(filename in dir (inputdir)) 
{
  print (filename)
  InputPath = sprintf ("%s/%s", InputDir, fileName)
  data = read.table (InputPath, Header = TRUE)
  cnames = Colnames (da TA)
  cnames[1] = "DEPTH"
  cnames[cnames = = "DEVI"] = "Dev"
  cnames[cnames = = "DV"]   = "Dev"
  cnames[ CNAMEs = = "AZI"]  = "AZIM"
  cnames[cnames = = "AZ"]   = "AZIM"
  colnames (data) = cnames
  data = Data[c ("DEPTH", "AZIM", "DEV")]

  OutputPath = sprintf ("%s/%s", OutputDir, FileName)
  write.table (data, outputPath, row.names = FALSE, Sep = "\ t") 
 
  }

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.