datetime r abbreviated month
The locale would be the issue. One option would be to set the locale after checking
Sys.getlocale("LC_TIME")
If it is not English or US, then change it with Sys.setlocale
Sys.setlocale("LC_TIME", "English")
Getting an NA with abbreviated month format for as.Date in Spanish locale
It works for me. Make sure you are actually in a Spanish locale. (I am on Windows and it is possible that it works differently on other platforms.)
Sys.setlocale("LC_TIME", "English")
## [1] "English_United States.1252"
Sys.getlocale("LC_TIME")
## [1] "English_United States.1252"
format(Sys.Date(), "%b")
## [1] "Feb"
Now change to Spanish:
Sys.setlocale("LC_TIME", "Spanish")
## [1] "Spanish_Spain.1252"
Sys.getlocale("LC_TIME")
## [1] "Spanish_Spain.1252"
format(Sys.Date(), "%b")
## [1] "feb"
as.Date("01-feb-2021", format="%d-%b-%Y")
## [1] "2021-02-01"
as.Date("01-Feb-2021", format="%d-%b-%Y")
## [1] "2021-02-01"
How to change the format of abbreviated month with a ending dot in my software R?
I had exactly the same problem yesterday. I just added the following line
Sys.setlocale("LC_TIME", "C")
fecha<-c("06-jul-2015","06-sep-2012")
as.Date(fecha, format="%d-%b-%Y")
and it works:
"2015-07-06" "2012-09-06"
Edit: for the Spanish and the dot in your comment:
Sys.setlocale("LC_TIME","Spanish")
fecha<-c("06-ene.-2015","06-dic.-2012")
as.Date(fecha, format="%d-%b.-%Y")
function as.Date just recognizes german month - but not english month
If you are 'completely sure' that the language is German try this:
Sys.setlocale("LC_ALL","German")
test <- c("15MAI2006","01OKT2011")
as.Date(test, format='%d%B%Y')
[1] "2006-05-15" "2011-10-01"
However, you have in your original data "01OCT2011" and October should be Okt
How To Properly change Strings To Month Abbreviations in R?
using dplyr
you might try this:
install.packages("dplyr")
library(dplyr)
mapoc_temp <- mapoc_temp %>%
mutate(month_new = month.abb[as.numeric(gsub("M","",month))])
The %>%
is an indicator of the pipline. month.abb is something from base R that contains all month abbreviations (note that it is the abbreviations for the language your R is in, you can change that if you want see for example). By using gsub I delete the M that is in front of the numbers. These numbers are used to select the correct month in month.abb.
It results in:
Longitude Latitude Temp month year month_new
1 -43.54116 59.95063 -1.1657088 M1 2016 Jan
2 -43.54116 59.95063 -1.7090803 M1 2017 Jan
3 -43.54116 59.95063 -1.7090803 M1 2018 Jan
4 -43.54116 59.95063 -1.6484648 M2 2016 Feb
5 -43.54116 59.95063 -1.5090311 M2 2017 Feb
6 -43.54116 59.95063 -1.5090311 M2 2018 Feb
7 -43.54116 59.95063 -1.2948184 M3 2016 Mar
8 -43.54116 59.95063 -0.8193197 M3 2017 Mar
9 -43.54116 59.95063 -0.8193197 M3 2018 Mar
10 -43.54116 59.95063 0.9379213 M4 2016 Apr
If you don't want it in a new variable, just change the month_new to month. But this is for display purposes that the month M# valuse are correctly converted.
Related Topics
Remove the Columns with the Colsums=0
Removing Everything After First 'Backslash' in a String
Shiny - Custom Warning/Error Messages
Difference of Two Character Vectors with Substring
Stopping the Script Until a Value Is Entred from Keyboard in R
Calculating Inter-Purchase Time in R
R - Pivoting Duplicate Rows into Multiple Column with Unknown Number of Columns
R: Split String into Numeric and Return the Mean as a New Column in a Data Frame
Grouping Factor Levels in a Data.Table
R Geom_Tile Ggplot2 What Kind of Stat Is Applied
Data.Table: Sum by All Existing Combinations in Table
How to Draw Roc Curve Using Value of Confusion Matrix
How to Color Bar Plots When Using ..Prop.. in Ggplot
R - Identify Consecutive Sequences
Calculating the Distance Between Points in Different Data Frames