r错误中的错误()`:!计算'年=年度(用户_ year)````''。由`as.posixlt.numeric()`::! ' rigins'必须提供

发布于 2025-02-10 23:29:55 字数 944 浏览 3 评论 0原文

我知道这个问题已经很多次,但是由于某种原因,我仍然会遇到这个错误,尽管我应该根据这个论坛上的答案来起作用。也许我错过了一步。

基本上,我想将user_yearnum更改为 date,因此传单>传单 MAP LEGEND不显示一年使用逗号

示例数据(df):

structure(list(USER_Date = structure(c(18996, 18633, 19011, 19012, 
19016, 19019, 19020, 19011, 19023, 19023), class = "Date"), USER_Year = c(2022, 
2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022), USER_Month = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1), USER_Day = c(4, 6, 19, 20, 24, 27, 
28, 19, 31, 31)), sfc_columns = c("x", "y"), class = "data.frame", row.names = c(NA, 
10L))

代码:

library(tidyverse)

# User lubridate
df = df %>%  mutate(Year = year(USER_Year))

错误:

Error in `mutate()`:
! Problem while computing `Year = year(USER_Year)`.
Caused by error in `as.POSIXlt.numeric()`:
! 'origin' must be supplied

I know this question has been asked a lot of times but for some reason I still get this error even I though it should work based on the answers on this forum. Maybe I am missing a step.

Basically, I would like to change USER_Year from num to date so the leaflet map legend does not show the year with commas.

Sample Data (df):

structure(list(USER_Date = structure(c(18996, 18633, 19011, 19012, 
19016, 19019, 19020, 19011, 19023, 19023), class = "Date"), USER_Year = c(2022, 
2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022), USER_Month = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1), USER_Day = c(4, 6, 19, 20, 24, 27, 
28, 19, 31, 31)), sfc_columns = c("x", "y"), class = "data.frame", row.names = c(NA, 
10L))

Code:

library(tidyverse)

# User lubridate
df = df %>%  mutate(Year = year(USER_Year))

Error:

Error in `mutate()`:
! Problem while computing `Year = year(USER_Year)`.
Caused by error in `as.POSIXlt.numeric()`:
! 'origin' must be supplied

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2025-02-17 23:29:55

我建议改用年份转换为角色。这应该与传单一起使用。

## Libraries
library(dplyr)
library(lubridate)

df$USER_Year1 = as.character(df$USER_Year)
str(df)

输出

> df$USER_Year1
 [1] "2022" "2021" "2022" "2022" "2022" "2022" "2022" "2022" "2022" "2022"
> df
    USER_Date USER_Year USER_Month USER_Day USER_Year1
1  2022-01-04      2022          1        4       2022
2  2021-01-06      2021          1        6       2021
3  2022-01-19      2022          1       19       2022
4  2022-01-20      2022          1       20       2022
5  2022-01-24      2022          1       24       2022
6  2022-01-27      2022          1       27       2022
7  2022-01-28      2022          1       28       2022
8  2022-01-19      2022          1       19       2022
9  2022-01-31      2022          1       31       2022
10 2022-01-31      2022          1       31       2022
> str(df)
'data.frame':   10 obs. of  5 variables:
 $ USER_Date : Date, format: "2022-01-04" "2021-01-06" "2022-01-19" "2022-01-20" ...
 $ USER_Year : num  2022 2021 2022 2022 2022 ...
 $ USER_Month: num  1 1 1 1 1 1 1 1 1 1
 $ USER_Day  : num  4 6 19 20 24 27 28 19 31 31
 $ USER_Year1: chr  "2022" "2021" "2022" "2022" ...
 - attr(*, "sfc_columns")= chr [1:2] "x" "y"

I'd recommend converting the year to character instead. This should work with leaflet.

## Libraries
library(dplyr)
library(lubridate)

df$USER_Year1 = as.character(df$USER_Year)
str(df)

Output

> df$USER_Year1
 [1] "2022" "2021" "2022" "2022" "2022" "2022" "2022" "2022" "2022" "2022"
> df
    USER_Date USER_Year USER_Month USER_Day USER_Year1
1  2022-01-04      2022          1        4       2022
2  2021-01-06      2021          1        6       2021
3  2022-01-19      2022          1       19       2022
4  2022-01-20      2022          1       20       2022
5  2022-01-24      2022          1       24       2022
6  2022-01-27      2022          1       27       2022
7  2022-01-28      2022          1       28       2022
8  2022-01-19      2022          1       19       2022
9  2022-01-31      2022          1       31       2022
10 2022-01-31      2022          1       31       2022
> str(df)
'data.frame':   10 obs. of  5 variables:
 $ USER_Date : Date, format: "2022-01-04" "2021-01-06" "2022-01-19" "2022-01-20" ...
 $ USER_Year : num  2022 2021 2022 2022 2022 ...
 $ USER_Month: num  1 1 1 1 1 1 1 1 1 1
 $ USER_Day  : num  4 6 19 20 24 27 28 19 31 31
 $ USER_Year1: chr  "2022" "2021" "2022" "2022" ...
 - attr(*, "sfc_columns")= chr [1:2] "x" "y"
遗忘曾经 2025-02-17 23:29:55

约会总是有一年,月和一天。因此,您将必须选择这样的随机日子:

df <- df %>%  
  mutate(Year = as.Date(paste(USER_Year, 1, 1, sep = "-")))

或者更好,使用实际的月和日值:

df <- df %>%  
  mutate(Year = as.Date(paste(USER_Year, USER_Month, USER_Day, sep = "-")))

然后使用格式(DF $年,“%y”)仅从“日期

A date always has a year, month and day. So you will have to pick a random day like this:

df <- df %>%  
  mutate(Year = as.Date(paste(USER_Year, 1, 1, sep = "-")))

or better, use the actual month and day values:

df <- df %>%  
  mutate(Year = as.Date(paste(USER_Year, USER_Month, USER_Day, sep = "-")))

Then use format(df$Year, "%Y") to only get the year from the date

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文