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")
}