Reading plot names from folder using gnuplot - plot

I want to make variable "list" containing filenames, variable "i" containing number of datafiles and plot it into single graph. Example of filenames: ConvAut.dat, ConvMoveAut.dat, CutAut.dat ...
Here is my try:
list = system("echo $(dir *.dat)")
plot for [1:i] i using 1:((2*i)+(column(2))) w steps tit i
I just found this piece of code but i dont know how to edit it.
I have version 4.6 patchlevel 0. Can you help me?
Thanks for the answers

list = system("dir /b *.dat") # Windows
# list = system("ls *.dat") # Unix
plot for [i=1:words(list)] word(list, i) using 1:((2*i)+column(2)) w steps title word(list, i)
The list is a string which contains all files, separated by white spaces. In the plot command you could also iterate over this file list with plot for [file in list] file ..., but then you don't have access to the file number. So I used words to get the number of files, and word(list, i) to get the i-th file name.
Note, that this works only if the file names don't contain white spaces.

Related

Why is xmgrace ignoring my column of X data?

I've been a longtime user of xmgrace/grace, but today I'm stumped. I have a .dat file that is simply two columns of space-separated X Y data like I'm accustomed to plotting. But for some reason the program is only using the Y information and plotting X as the integer sequence/row numbers. Here's a sample of the data:
$ cat test.dat
0.016426149 0.91780442
0.016559154 1.942617893
0.016692159 1.937870622
0.016758662 2.160227537
0.016825165 1.464688301
0.016891667 2.413390636
0.017024672 2.62378788
0.017024672 2.396263838
0.01722418 2.30436182
but when I run xmgrace test.dat (or try Data > Import > ASCII...), the x-axis goes from 0 to 14 instead of 0.0164 to 0.0172.
I don't see any hidden characters in the file (used :set list in vi)...
If this helps: The file was originally a CSV that I exported from Excel on my Mac, and used vi to replace the commas with spaces. So...there are no tabs in the file, and no Windows-style ^M's.
How do I get xmgrace to use my x information properly?

Reading files from folder and storing them to dataframe in R

My goal eventually is to build a classifier -- something like a spam detector.
I am not sure however how to read the text files that contain the text that will feed the classifier and store it to a dataframe.
So suppose that I have assembled in a folder text files -- raw text initially stored in a Notepad and then saved from it in a txt file-- whose names are indicative of their content, e.g. xx_xx_xx__yyyyyyyyyyy_zzzzz, where xx will be numbers representing a date, yyyyyyyyy will be a character string representing a theme and zzzz will be a character string representing a source. yyyyyyyyyyy and zzzzzzz will be of variable lengths.
My objective would be to create a function that would loop through the files, read them, store the information contained in their names in separate columns of a dataframe --e.g. "Date", "Theme", "Source" -- and the text content in a fourth column (e.g. "Content").
Any ideas how this could be achieved?
Your advice will be appreciated.
Hi here is a possible answer, I'm storing results in a list instead of a data frame but you can convert from one to the other by using do.call(rbind.data.frame, result)
require(stringr)
datawd<-"C:/my/path/to/folder/" # your data directory
listoffiles<-list.files(str_c(datawd)) # list of files
listoffiles<-listoffiles[grep(".txt",listoffiles)] # only extract .txt files
my_paths<-str_c(datawd,listoffiles) # vector of path
# the following works with windows only
progress<-winProgressBar(title = "loading text files",
label = "progression %",
min = 0,
max = length(my_paths),
initial = 0,
width = 400)
#000000000000000000000000000000000000000 loop
for (i in 1:length(chemins)){
result<-list()
setWinProgressBar(progress,i,label=listoffiles[i])
the_date<-sapply(strsplit(listoffiles[i],"_"),"[[",1)
the_theme<-sapply(strsplit(listoffiles[i],"_"),"[[",2)
the_source<-sapply(strsplit(listoffiles[i],"_"),"[[",3)
# open connexion with read
con <- file(my_paths[i], open = "r")
# readlines returns an element per line, here I'm concatenating all,
#you will do what you need....
the_text<- str_c(readLines(con,warn = FALSE))
close(con) # closing the connexion
result[[i]]<-list()
result[[i]]["date"]<-the_date
result[[i]]["source"]<-the_source
result[[i]]["theme"]<-the_theme
result[[i]]["text"]<-the_text
}
#000000000000000000000000000000000000000 end loop

remove multiple sequenced files in R

i created multiple files named 1:100 + random letter to file:
for (i in 1:100){
file.create( paste0(i , ".txt"), showWarnings=TRUE)
# assign random LETTER to files
AZ <- sample(LETTERS,1)
cat(AZ,file = paste0(i,".txt"),append=TRUE)
#rename files, and create new file with append of LETTERS
name <- scan(file=paste0(i,".txt"), what="character")
file.rename(paste0(i,".txt"), paste0(i, name,".txt"))
Now, i have a lot of files named like "1T, 2C, 3Y,..., 100A" and i want to remove all these files (not removing the rest that has in the directory) with file.remove function, how should i remove them without naming one by one? and all the directory named "exercicio03" with everything inside?
ps.: i have already tried
file.remove(paste0(i,name,".txt"))
but is removing only the last file "100A"
You can easily remove only the files with names like "1T.txt, 2C.txt, 3Y.txt, ..., 100A.txt" with the following two lines of code:
remove.files <- list.files(".", pattern="^[0-9]{1,3}[A-Z]{1}\\.txt$")
do.call(file.remove,list(remove.files))
The script obtains all text files beginning with 1-3 digits followed by a letter in the current directory where you created them, and removes them.
Since you used a sample function, I think you can only be 100% sure that you remove only these files and no others, if you (did) save the values you became from that sample function.
So your first part should have been:
AZ<-NA
for (i in 1:100){
file.create( paste0(i , ".txt"), showWarnings=TRUE)
# assign random LETTER to files
AZ[i] <- sample(LETTERS,1)
cat(AZ[i],file = paste0(i,".txt"),append=TRUE)
#rename files, and create new file with append of LETTERS
name <- scan(file=paste0(i,".txt"), what="character")
file.rename(paste0(i,".txt"), paste0(i, name,".txt"))
}
That way you can afterwards remove them all via this :
for (i in 1:100){
file.remove(paste0(i,AZ[i],".txt"))
}

How to output a file name based on a file chosen in R

Hi I am performing a task on a text file chosen from file.choose which just separates out the lines and then adds a new number to each separation.
I would like to name the outputted file the same as file.choose but instead of .txt I would like it to be called .fa. So far I have
fileConn<-file("outputtbb.txt")
longlist <- readLines(file.choose())
lvls1 <- unique(longlist)
cat(paste0("Sequence>", seq_along(lvls1), "\n", lvls1, collapse="\n"), file=fileConn)
close(fileConn)
I know its something to do with changing file=fileConn but cant figure out how to manipulate it and all I get is outputtbb.
Please help!
You could seperate the readLines(file.choose()) to two lines.
filename <- file.choose()
longlist <- readLines(filename)
You now have file name available as a variable and you can bend it, you can twist it all day long (no reference to iPhone 6 bending intended).
To change endings, assuming no ".txt" appears anywhere else in the file name, this would work
x <- "file.txt"
sub(".txt", replacement = ".fa", x = x)
[1] "file.fa"

How to force R to read in numerical order?

I have several files in one folder and I want to rename them,I noticed that R reads in alphbatical order, so I used the command mixedsort and it worked but when I checked the results I found that the files were read in a different order not numerically. The name of the first file is Daily_NPP1.bin up to Daily_NPP365.bin
a<- list.files("C:\\New folder (6)", "*.bin", full.names = TRUE)
k<- mixedsort(a)#### load package feild
b <- sprintf("C:carbonflux\\Daily_Rh%d.bin", seq(k))
file.rename(a, b)
How do I force R to read in numerical order?
If renaming is all you want to do you could just do the following, regardless of sorting.
b <- sub("^.*?([0-9]+)\\.bin$", "C:\\\\carbonflux\\\\Daily_Rh\\1.bin", a)
file.rename(a, b)
The first argument to sub extracts the numbers at the end of the file names, and the second pastes it into the new file name template (at the position of \\1). All the \\\\ are needed to escape the backslashes properly.
Here is a way to order the vector without renaming the files:
# Replication of data:
a <- sort(paste0("Daily_NPP",1:365,".bin"))
# Extract numbers and order:
a <- a[order(as.numeric(gsub("[^0-9]","",a)))]

Resources