Hello !
I am a newbie on R and i am face a problem that i hope someone here can help me ^^
I have several .txt document whose i want to study
I import and create several data frame for each document
folder <- "/Users/sylvain/Desktop/php/linking-interne-semantique/" # path to folder that holds multiple .csv files
files_names3 <- list.files(path="/Users/sylvain/Desktop/php/linking-interne-semantique", pattern="*.txt")
for (i in 1:length(files_names3)){
assign(files_names3[i],
read.delim(files_names3[i])
)}
I have in total, 33 documents, now i want to create a data frame, which each document will be compare each other.
Example : i have, " doc1.txt ", " doc2.txt ", "doc3.txt"
i need a result similar to :
doc1.txt doc1.txt
doc1.txt doc2.txt
doc1.txt doc3.txt
doc2.txt doc1.txt
doc2.txt doc2.txt
doc2.txt doc3.txt
doc3.txt doc1.txt
doc3.txt doc2.txt
doc3.txt doc3.txt
I have this code :
for (i in 1:length(files_names3)) {
for (j in 1:length(files_names3)) {
print(paste(files_names3[i],files_names3[j],sep=","))
}
}
When i print, it works, but i dont know how to set the whole resultat to a dataframe
I found the problem , i had to add " df " on expand.grid !
And have something like :
-> expand.grid.df(doc2,doc2)
^^
But, i am still curious to try to do it with loop, in php, we can do it and it works, but not in R, it weird,
don't you think ?
The whole code :
#preface : i have several .txt document on a folder
#I need to create a data frame for each document .txt
folder <- "/Users/sylvain/Desktop/php/linking-interne-semantique/" # path to folder that holds multiple .csv files
files_names3 <- list.files(path="/Users/sylvain/Desktop/php/linking-interne-semantique", pattern="*.txt")
for (i in 1:length(files_names3)){
assign(files_names3[i],
read.delim(files_names3[i])
)}
# Now about the loop
#When i print it, it works ! i have the same result as expand.grid.
for (i in 1:length(files_names3)) {
for (j in 1:length(files_names3)) {
print(paste(files_names3[i],files_names3[j],sep=","))
}
}
#So i tried now to put all of that on a data frame, in order to have something like (if i have 3 documents) :
doc1.txt doc1.txt
doc1.txt doc2.txt
doc1.txt doc3.txt
doc2.txt doc1.txt
doc2.txt doc2.txt
doc2.txt doc3.txt
doc3.txt doc1.txt
doc3.txt doc2.txt
doc3.txt doc3.txt
data <- ''
for (i in 1:length(files_names3)) {
for (j in 1:length(files_names3)) {
for (k in 1:9) {
data[k] <- print(paste(files_names3[i],files_names3[j],sep=","))
}
}
}
I have as result :
doc 1 doc 1
doc 1 doc 1
doc 1 doc 1
doc 1 doc 1
This is not a reprex. Please go through the link I shared earlier. Also, please format your code while posting.
You can create the data frame via for loops, but I guess you'll have to do it via matrix. The reason your code fails is that you are printing the output of paste, instead of storing it. Also, data <- '' is a wrong thing to do.