I'm having some challenge converting a newly created column in r to date format

I have recently merged two ".txt" files and have the dates separated into 3 different columns - "YYYY", "MMM" and "DD" and the month is in the format "October."

I have successfully merged the 3 columns into 1 column - "YYYY" "MMM" and "DD" but when I try to convert to date format using the code - date <- as.numeric(as.character(date)) , it comes up with the following error - Error in as.character(date) :
cannot coerce type 'closure' to vector of type 'character'

Kindly help

date is also the name of a function. A closure is a function that is mentioned without its arguments. Along with df (despite being used commonly for examples) data, c and many others names, sometimes they work as hoped and sometimes not. Come up with substitutes, like my_date, dated, date_, Date to avoid the problem.

Given a string 2022-Dec-06 the following works smoothly to create an object of class Date

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
ymd("2022-Dec-06") |> str()
#>  Date[1:1], format: "2022-12-06"

Created on 2022-12-06 by the reprex package (v2.0.1)

This is really appreciated. it worked

1 Like

I have used the following code to generate a dummy variable but keep coming up with the same output - "0"

ifelse(nmerge_base$Day == 'Sat', 1, 0)

Please help

library(lubridate)
#> Loading required package: timechange
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
input <- ymd("2022-Dec-06")
ifelse(wday(input) == 3,1,0) # wday begins week on Sun
#> [1] 1
ifelse(wday(input) == 7,1,0)
#> [1] 0

Thank you for the feedback

I forgot to add that "Wed" and "Sat" are elements in a "Days" column in the dataframe

Using a date object and pulling out components such as day of week, month, year, week of year or quarter of year has advantages over dealing with string processing. But, if you want help, post a reprex so it will be possible to see what’s going on.

Thank you. Please see a reprex below:

Day of the week
Wed
Sat
Sat
Sat
Wed

#I want to create a new column of dummy variables which produce a "1" for "Wed" and "0" for "Sat" thereby producing the output below

Day of the week WedV (dummy)
Wed 1
Sat 0
Sat 0
Sat 0
Wed 1

apologies the table scattered. The dummy variable is a separate column of "1" and "0"

A reprex looks like this

my_data <- data.frame(dow = c("Wed", "Sat", "Sat", "Sat", "Wed"))
my_data$dummy <- ifelse(my_data$dow == "Wed",1,0)
my_data
#>   dow dummy
#> 1 Wed     1
#> 2 Sat     0
#> 3 Sat     0
#> 4 Sat     0
#> 5 Wed     1

This snippet depends critically on the assumption that anything other than Wed should evaluate to 0.

Thanks for sharing

This above format is what I have followed but it keeps returning only "1" for either Sat or Wed.

I already have a dataframe with a column called "Day" and I use the following

my_data$new_column <- ifelse(my_data$Day == 'Sat', 1, 0)

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:


Short Version

You can share your data in a forum friendly way by passing the data to share to the dput() function.
If your data is too large you can use standard methods to reduce it before sending to dput().
When you come to share the dput() text that represents your data, please be sure to format your post with triple backticks on the line before your code begins to format it appropriately.

```
( example_df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 
3.4, 2.9, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 
1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 
0.4, 0.3, 0.2, 0.2, 0.1), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")))
```

Did you cut-and-paste my reprex?

I just followed the command syntax since I already had a column

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.