Reputation: 1014
It is the first time that I am using R since I need to create many plots for each file that I have.
I have a CSV file with four columns and I want to generate a histogram for each file and save it in a directory. I could read the files and attach a data frame to them. but when I run, it seems the code stuck somewhere, neither generate Error nor the plots! Data look like this
t1;w1;tt;probability
0;0.04745911973818716;reflections;0.0005
0;0.04745911973818716;clouds;0.001
0;0.04745911973818716;tree;0.14
Here is my code:
#list files
temp = list.files("myDirectory",pattern ="*.csv")
path <-
file.path("myDirectory")
for (i in 1:length(temp)){
data <- read.csv(file.path(path, temp[i] ), sep = ";" )
mypath <- file.path("C:","folder1","folder2","plots",paste("myplot_", i,".tiff", sep = ""))
tiff(file=mypath, width = 8, height = 8,units = 'in',res=300)
hist(c(data$probabilities,data$words), breaks= 0:1, main="word probabilities",xlab="words")
dev.off()
}
Upvotes: 0
Views: 71
Reputation: 8846
Are you sure what you really want is a histogram?
If your .csv files contains more than a few dozen rows and you want to plot the distributions of probabilities, that's likely the case. If there are fewer rows, and you want to plot the probability of each individual word, you're likely looking for a barplot.
Just in case, here are examples of both. I simplified a few things to make it entirely self-contained.
First creating a directory named "so-test" and add a couple of .csv files to it.
dir.create(testdir <- "so-test")
write("t1;w1;tt;probability
0;0.04745911973818716;reflections;0.05
0;0.04745911973818716;clouds;0.02
0;0.04745911973818716;tree;0.14",
file.path(testdir, "so-test1.csv"))
write("t1;w1;tt;probability
0;0.04745911973818716;cramp;0.007
0;0.04745911973818716;practice;0.009
0;0.04745911973818716;brush;0.02",
file.path(testdir, "so-test2.csv"))
Get a list of .csv files present in the directory
temp <- list.files(testdir, pattern=".csv$"); temp
path <- file.path(testdir)
Create a histogram over the probabilities.
It looks a bit weird as the number of probabilities is only three.
for (i in 1:length(temp)) {
data <- read.csv(file.path(path, temp[i]), sep = ";")
mypath <- file.path(path, paste("hist_", i, ".png", sep=""))
png(file=mypath, width=8, height=8, units='in', res=300)
hist(data$probability, main="word probabilities",
xlab="probability")
dev.off()
}
Create a barplot displaying the probability associated with each word.
for (i in 1:length(temp)) {
data <- read.csv(file.path(path, temp[i]), sep = ";")
mypath <- file.path(path, paste("barplot_", i, ".png", sep=""))
png(file=mypath, width=8, height=8, units='in', res=300)
barplot(data$probability, names.arg=data$tt,
main="word probabilities", ylab="probability")
dev.off()
}
Upvotes: 1