0

I have a weird error message popping up in a markdown in R. I am running this line of code heartrate_data[heartrate_data == "2022484408"] <- "user_6"

Error in as.POSIXlt.character(x, tz, ...): character string is not in a standard unambiguous format

I do not understand as I am not working with dates or attempting to use posix.. I am wondering if R is trying to pick up the numbers as a date in posixlt? How do I get rid of this error.

structure(list(Id = c(2022484408, 2022484408, 2022484408, 2022484408, 
2022484408, 2022484408, 2022484408, 2022484408, 2022484408, 2022484408
), Time = c("4/12/2016 7:21:00 AM", "4/12/2016 7:21:05 AM", "4/12/2016 
7:21:10 AM", 
"4/12/2016 7:21:20 AM", "4/12/2016 7:21:25 AM", "4/12/2016 7:22:05 AM", 
"4/12/2016 7:22:10 AM", "4/12/2016 7:22:15 AM", "4/12/2016 7:22:20 AM", 
"4/12/2016 7:22:25 AM"), Value = c(97L, 102L, 105L, 103L, 101L, 
95L, 91L, 93L, 94L, 93L)), row.names = c(NA, 10L), class = "data.frame")

Thank you

Stackstudent_09
  • 131
  • 1
  • 9
  • What does `class(heartrate_data)` return? It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 24 '22 at 21:39
  • @MrFlick numeric, I included dput of this code. Is there anything else that would be helpful in this situation in regards to reproducible example? – Stackstudent_09 May 24 '22 at 21:50
  • So you posted `dput(heartrate_data)`. That should Rajeev returned a class of data.frame, not numeric if that’s the case. What are you trying to do wjth `heartrate_data==“2022484408”`? Are you trying to match the ID column? Because your Time column is a posix date time value. You aren’t clearly indexing either column which is odd. – MrFlick May 24 '22 at 22:00
  • I am attempting to change all "2022484408" Id values in heartrate_data to be "user_6" instead of the string of numbers. I did class(heartrate_data$Id) and received numeric class(heartrate_data) is data frame. my bad. – Stackstudent_09 May 24 '22 at 22:01
  • tag name for above comment @MrFlick – Stackstudent_09 May 24 '22 at 22:32

1 Answers1

1

Too long for a comment.

First, you seem to want to reference the Id column, but your filter expression references the whole data.frame.

Second, your Id column, as defined, is integer, not character so the filter condition will not work.

Third, you index a data.frame using, e.g., df[<rowfilter>, ] (note the comma after the filter condition.

So try this:

heartrate_data[heartrate_data$Id==2022484408, ]$Id <- 'user_6'

or

heartrate_data$Id[heartrate_data$Id==2022484408] <- 'user_6'

The first expression extracts the rows of heartrate_data meeting the filter condition and sets the $Id column in that subset to 'user_6'.

The second expression references the Id column explicitly as a vector and sets all elements of that vecotr meeting the filter condition to 'user_6'. In this case they both give the same result.

In either case the class of the Id column will be changed from integer to character and this change will apply to all the rows (including those not selected by the filter condition).

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Cool thank you, so when you say the Id column will be changed from integer to character does this happen automatically with the expression? @jlhoward – Stackstudent_09 May 25 '22 at 21:09
  • Yes. R does not have strong typing, so the class (data type) of a vector, or a matrix, or a column of a `data.frame` will be changed automatically. Try `x <- 1:10; class(x); x[2] <- 'a'; class(x)`. – jlhoward May 25 '22 at 22:21