0

I have the following in a CSV file and I want to have them the same format, below, in R

01:00:00 02:00:00 03:00:00
15       80       67
30       44       2

I've been able to put them in a single variable

textus="01:00:00 02:00:00 03:00:00 

15 80 67 

30 44 2"

Then

data <- unname(as.matrix(read.table(text = textus)))

After, I'm trying to put them together with the following command line:

data1 <- rep(data[1,],data[2,],data[3,])

But it keeps giving me the following error lines:

Message d'avis :
In rep(dat[1, ], dat[2, ], dat[3, ]) :
seul le premier élément de l'argument 'length.out' est utilisé

The error is in french, to make things worse.

I've tried the following and it works even if it is only doing half of what I wish R to do:

data1<-rep(dat[1,],dat[2,]) 

# results
01:00:00 02:00:00 03:00:00
15       80       67

data2<-rep(dat[1,],dat[3,])

# results
01:00:00 02:00:00 03:00:00
30       44       2

If you have any input of what I'm missing, I will be glad to listen.

Best.

Andy K
  • 4,944
  • 10
  • 53
  • 82
  • side note: see this on how to [change language error](http://stackoverflow.com/questions/13575180/how-to-change-the-language-of-errors-in-r) – agstudy Aug 02 '13 at 10:17
  • 2
    Why `rep`? What is your expected output? Would `as.data.frame(t(data))` answer your question? – flodel Aug 02 '13 at 10:19
  • Hi Flodel, rep is working fine with the 2 lines things as shown here `data1<-rep(dat[1,],dat[2,])` `# results 01:00:00 02:00:00 03:00:00 15 80 67 ` I will try your method – Andy K Aug 02 '13 at 10:23
  • Hi Flodel, not perfect but I can use a reshape afterwards. Thanks. – Andy K Aug 02 '13 at 10:32
  • No, `rep` is for a totally different usage. You do not have a `dat` object but a `data` one. And `rep(data[1,],data[2,])` gives something totally different. – flodel Aug 02 '13 at 10:33
  • What you need to realize is that 1) you have data of different types, (strings or dates, integers) 2) R's objects for this kind of data are data.frames 3) data.frames are column wise objects where each column holds a different type of object. So you should store the transpose of your data in a data.frame. Easiest would be if the process creating your csv could store its transpose instead, and with a header, so you would only have to use `read.csv` on it. – flodel Aug 02 '13 at 10:36
  • `dat` is the name of my variable. I agree with you that I have `data` here. My aim was to have one hour (e.g 1AM) for one column and on the next row in the same column, the number of registrations. And that goes on for the next column ... – Andy K Aug 02 '13 at 10:38
  • flodel, this is what I've done firsthand, have a CSV with a header but alas it did not work. This is why I've done it that way. Time consuming, yes. Could have done through another way. yes. But I'm learning : ) Andy – Andy K Aug 02 '13 at 10:50

1 Answers1

1

If you don't mind writing to a file in between, this would be the simplest way:

tmp <- read.table(text=textus)

write.table(t(tmp), "tmp.csv", row.names = FALSE, col.names = FALSE, sep = ",")

read.table("tmp.csv", sep = ",")
#         V1 V2 V3
# 1 01:00:00 15 30
# 2 02:00:00 80 44
# 3 03:00:00 67  2

There is no reason to have this transposed (as in the input file) in R. R's data.frame structure is designed to have variables in columns and observations in rows and most functions are optimized for this. If you just want to have it printed in the transposed form you should modify print.data.frame.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Hi Roland, thanks for your answer. You said that good practice mean that variables are in columns and observations in rows. However, when I'm plotting in such a way, I'm not having a plot with 2 curves. And my aim, is to have a plot. If you have any insights about best pratices, I'm wiling to hear. Thanks in advance, Andy – Andy K Aug 02 '13 at 12:30
  • There are numerous possibilities to plot the data. `matplot(as.POSIXct(df[,1], format="%H:%M:%S"), df[,-1], type="l")` should get you started. Read some introductions to R in general and to plotting with R specifically. – Roland Aug 02 '13 at 12:37
  • Ok ... never heard of that one and did not know you could do that ... (baffled) and suddenly feeling very lame and not knowledgeable ... – Andy K Aug 02 '13 at 12:45
  • @AndyK Yes, Grasshopper, knowing that you do not know is the first step on the path to enlightenment. :-) . So go do the reading and experiment with different packages and functions; you'll get the hang of it pretty soon. – Carl Witthoft Aug 02 '13 at 14:40