0

I've got a question about why does something happen. So, if I do something like this:

prices <- list()
for (i in 1:4){
   prices[[i]] <- read.csv(file.choose())
}
prices.df <- as.data.frame(prices)

the prices.df looks like... this:

 c.58.846..57.36..57.523..57.538..58.83..52.174..59.195..64.529..
1                                                           58.846
2                                                           57.360
3                                                           57.523
4                                                           57.538
5                                                           58.830
6                                                           52.174

But if I do something like that:

prices <- as.list(c(1:4))
names(prices) <- paste("data", 1:4)
for (i in 1:4){
    prices[[i]] <- read.csv... 
}
prices.df <- as.data.frame(prices)\

Then it looks ok, so like this:

    data.1  data.2
1   58.846  37.830
2   57.360  39.310
3   57.523  43.515
4   57.538  45.080
5   58.830  42.910
6   52.174  45.560 

Why is that? I don't get it really...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Can you please link to the file or provide a tiny data set to use? use `dput` to make an easy-to-paste data set. [link here](https://meta.stackoverflow.com/questions/315885/what-is-the-correct-way-to-share-r-data-on-stackoverflow#comment301364_315885) – Kamil Feb 09 '18 at 22:28
  • Try `do.call("cbind", prices)`? Note this only works if the number of rows is the same in each file. – Mako212 Feb 09 '18 at 22:29
  • This answer may help too: https://stackoverflow.com/a/33073145/4421870 – Mako212 Feb 09 '18 at 22:33
  • @Kamil the data used is downloaded csv from stooq.com. I don't know if I can help you more, because the "read.csv" function is in the loop. – TheEditingify Feb 09 '18 at 23:28
  • @Mako212 Same amount of observations in each file, of course. But I just want to know why my first idea isn't working properly :D – TheEditingify Feb 09 '18 at 23:28
  • Look at `names(prices)` after your first way... – Gregor Thomas Feb 09 '18 at 23:51

0 Answers0