-1

This is my function:

x<-read.table(....., stringsAsFactors=FALSE, header=T);
x;

   Timestamp  time Barcode
1  11/1/2013  8:25   M01.A
2  11/1/2013  8:25   M01.B
3  11/1/2013  8:43   M02.A
4  11/1/2013  8:43   M02.B
5  11/1/2013  9:03   M03.A
6  11/1/2013  9:03   M03.B
7  11/1/2013  9:18   M04.A
8  11/1/2013  9:18   M04.B

dput(head(x));

structure(list(Timestamp = c("11/1/2013", "11/1/2013", "11/1/2013", 
"11/1/2013", "11/1/2013", "11/1/2013"), time = c("8:25", "8:25", 
"8:43", "8:43", "9:03", "9:03"), Barcode = c("M01.A", "M01.B", 
"M02.A", "M02.B", "M03.A", "M03.B")), .Names = c("Timestamp", 
"time", "Barcode"), row.names = c(NA, 6L), class = "data.frame")

target<- x$Timestamp;
t<- strptime(x$Timestamp, format = "%m/%d/%Y");
t;
[1] "2013-11-01" "2013-11-01" "2013-11-01" "2013-11-01" "2013-11-01" "2013-11-01" "2013-11-01" "2013-11-01"
for (i in seq_along(target)){
x$Timestamp[x$Timestamp == target[i]]<- t[i]
};

I am trying to covert a date using strptime, and it works, now I am trying to take this vector of indices and replace this column x$Timestamp. Can anyone tell me what I am doing wrong? I just dont see it.

This is the error I get:

Error in $<-.data.frame(*tmp*, "Timestamp", value = list(sec = c(0, : replacement has 9 rows, data has 78

Chad
  • 149
  • 4
  • 11

1 Answers1

0

This is a stab in the dark since no data or error message was provided.

However, t is the same length as x$Timestamp so you don't need to do these gymnastics. Instead, you can just replace directly:

x$Timestamp <- t

Or more succinctly:

x$Timestamp <- strptime(x$Timestamp, format='%m/%d/%Y')
Justin
  • 42,475
  • 9
  • 93
  • 111
  • @ Justin, this is the error I am getting when I use this piece of code: x$Timestamp <- strptime(x$Timestamp, format='%m/%d/%Y'). Error in `$<-.data.frame`(`*tmp*`, "Timestamp", value = list(sec = c(NA_real_, : replacement has 9 rows, data has 78 – Chad Jan 02 '14 at 19:32
  • Please include `dput(x)` or a portion of it `dput(head(x))` that reproduces your error and I'm happy to help. Otherwise, I don't have any way of knowing why your code does or does not work. – Justin Jan 02 '14 at 19:37
  • @ Justin, when I run my code this is what error I am getting Warning messages: 1: In `[<-.factor`(`*tmp*`, x$Timestamp == target[i], value = list( : invalid factor level, NAs generated 2: In x[...] <- m : number of items to replace is not a multiple of replacement length } Error: unexpected '}' in "}" – Chad Jan 02 '14 at 19:39
  • Which is why I asked you to use `dput`... use `read.table(..., stringsAsFactors=FALSE)`. – Justin Jan 02 '14 at 19:57
  • never heard of dput before. Anyways I have done what you said and now it shows this error: Error in `$<-.data.frame`(`*tmp*`, "Timestamp", value = list(sec = c(0, : replacement has 9 rows, data has 78 – Chad Jan 02 '14 at 20:12
  • anyways thinks for time and help at least – Chad Jan 02 '14 at 20:12
  • That error is not reflected in the data you've supplied in your post. The method I first suggested will work. Please make your question reproducible using dput as well as one that reproduces the error... – Justin Jan 02 '14 at 20:23
  • @ Justin, I am going to be honest here, I have no idea what you mean by dput. I used ?dput in R and this doesnt look very helpful. If you were to look at my question, I have updated the error. What you supplied earlier didnt work. I tried that simple solution first, and when to the fancy gymnastics as you call it... – Chad Jan 02 '14 at 20:28
  • you used `dput` correctly. It lets us see exactly what your input data looks like. However, using the portion you've supplied, there is no error if you run your code and use my answer suggestion. Thus, I cannot help since I don't get the error you're getting. – Justin Jan 02 '14 at 20:31
  • @ Justin, so I just realized that t has 78 dates, but R thinks it has a length of 9. Any clues? – Chad Jan 02 '14 at 20:57
  • I assume it still a `factor`, aka it has 9 unique `levels`. Please please please [read this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for any future questions you post here. It will save you and us a lot of time. – Justin Jan 02 '14 at 21:17