How to read CSV files in a loop? [closed] - r

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am new to R and now I have an issue while I try to read CSV files in a loop. My CSV files are named as
result_file_1.csv , result_file_2.csv,....result_file_10.csv
So I planned to read a CSV by the below code:
for(i in 1:10){
t1=read.csv("result_file_i.csv")
// rest of my code
}
I also tried:
for(i in 1:10){
t1=read.csv("result_file_"+i+".csv")
// rest of my code
}
both did not work. Any help is appreciated

As suggested above I used paste0("result_file",i,".csv") and it worked fine.

Related

R cannot export all rows to csv [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Fundamental stuff but i couldn't seem to get around this. I performed the following process:
d1<-read.csv('hourly.csv',sep=",",header=F)
names(d1)<-c("date","rain","q","qa","qb")
d2<-read.csv('event.csv',sep=",",header=F)
names(d2)<-c("enum","st","et","rain2","qtot")
for(k in 1:206){
st<-d2[k,2]
et<-d2[k,3]
Datetime<-d1[st,]
print(Datetime)
write.csv(Datetime, file="DatesA3.csv")
}
In the end, i exported the results to a csv file. There are 206 rows altogether and they display fine in R. But when exporting, only the last row is exported in the csv file. I tried multiple things such at write.table, append, etc. but nothing seems to work.
How do i export every row into one file?
Please advise and thank you!
Datetime[k, ] <- d1[st, ] # instead, otherwise you overwrite
# and write the result outside the loop

How to choose the first "n" elements in R [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
(USING R)
So I imported a data set by using
xcars <- read.csv(file.choose())
and then I chose my data set which was originally an excel file.
So, I have a column named dist (short for displacement) and I want to choose the first 25 entries underneath that column and then plot it on a histogram, so I attempted the following.
carsUpTo25 <- xcars(1:25,)
hist(carsUpTo25$dist)
Of course this didn't work. However, any help on how I would do this would be helpful.
Try this-
hist(xcars[,dist[1:10]])

Importing an excel file in RStudio [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Actually, I'm having trouble with importing a CSV file in my script in Rstudio. I am trying:
d <- read.table(file="table1.csv",sep="\t",header=T)
but it says that there is an error in file and "rt".
Type 'getwd()' in the console. If what returns isn't the same path as the file you're trying to read in, change this with setwd("[your filepath here]"). If you want a graphical solution instead, you can use the Set As / Go To Working Directory commands under the 'More' menu in RStudio's Files viewer (bottom right window).
Then try d <- read.csv("table1.csv")

"there is no package called 'xlsx'": having trouble reading .xlsx file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Looking to read in an .xlsx file. This is an assignment so I specifically can't use Excel first to convert it to .csv.
Tried using read.xlsx which couldn't be found so I tried 'library(xlsx)' which was not a package according to R.
Does anyone know if there's something glaringly obvious that I'm not doing? Is the package called something different? Is there a different/better way to read an xlsx file?
Thanks!

Why write of dataframe creates file with header and tail only? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a dataframe (data7) that has 12678 rows. When I write it to txt file I get in the file only the header and the tail of the dataframe. Why is it? How can I write all the content of the dataframe into txt file?
Here are the commands I use:
> nrow (data7)
[1] 12678
> location<-"C:/Data/"
> write(capture.output(data7), paste(location,"A123.txt"), append = TRUE)
Thanks to all commenters, Here is the solution:
> location<-"C:/Data/"
> write.table (data7, paste(location,"A123.txt"))

Resources