R date format of column based on criteria of other column


Sorry if this is a duplicate, I found many answers where you could change a value in a data frame to a number or string based on another column, however it doesn't seem to work when trying to change each value based on that value.

So I tried to use some answers for example I read R - Update a Value in One Column Based on Criteria in Other Columns

Now look at my data:

[1] "character"


[1] "2015-03-01T00:00:00Z"


[1] "2015-03-01"

So I know I can transform my data to what I want it to look like. However:

> df$a[df$b=="start"] <- as.Date(df$a[df$b=="start"])
> df[1,1]
[1] "16495"

I can change them all to a number of string, but when I try and do as.Date on each value it for some reason returns a strange number

I quickly tried:

lapply(df, function(x){if(df$b=="experience.start"){df$a<-as.Date(df$a)}})

Although the correct values were being printed to the console I don't know how to use this to edit my data frame.

Show source
| date   | R   | dataframe   | format   2017-01-06 16:01 2 Answers

Answers ( 2 )

  1. 2017-01-06 21:01

    Assuming I understand your data frame set up, rather than assigning the value to the "a" variable, make a new Date variable and assign to that.

    As Haboryme says, your existing column "a" is a character vector, and as.Date() returns a Date object, which is being converted-on-the-fly to a character -- notice the quotation marks around "16495".

    By assigning to a different variable, R will automatically make that a Date vector, and you also save your original information and make the code safer overall.

    The code below should work and will produce the same results even if you run it several times in a row.

    df$c <- as.Date(NA)
    df$c[df$b=="start"] <- as.Date(df$a[df$b=="start"])

    The code as you had it, even if it worked, would produce different results if run twice in a row:

  2. 2017-01-06 21:01

    To the best of my knowledge you can't because you are trying to have a column with values belonging to character and Date class. Nay you can resolve the problem in two ways.

    First Solution

    You can create another column newdates in this way:

    df$newdates <- as.Date(NA)
    df$newdates[df$b == "experience.start"] <- as.Date(df$a[df$b == "experience.start"])

    with this code you will have some row NA filled, because not all the rows have the b column equal to experience.start.

    Second Solution

    You can agree to a compromise and convert the Date in character:

    df$a[df$b == "experience.start"] <- as.character(as.Date(df$a[df$b=="experience.start"]))

    in this way, the type of the a column will be character and when b is equal to experience.start you'll find a date in this format: 2015-03-04 otherwise the format will be 2011-02-11T00:00:00Z.

◀ Go back