通过创建函数,代码错误来修改R列

发布于 2025-02-07 17:20:57 字数 1181 浏览 2 评论 0原文

我创建了这些行(函数)来修改数据框架的特定列,我想使用此函数将其用于不同的列和数据框架,但是该函数不起作用,我收到了错误代码消息。

change.date <-  function(df_date,col_nb,first.year, second.year){
  df_date$col_nb <- gsub(first.year, second.year,  df_date$col_nb)
  df_date$col_nb <- as.Date(df_date$col_nb)
  df_date$col_nb <-  as.numeric(df_date$col_nb)
    
}

change.date(df_2020,df_2020[1], "2020","2020")

Error in $<-.data.frame`(*tmp*`, "col_nb", value = character(0)):
replacement table has 0 rows, replaced table has 7265

我可复制的数据是:

df_2020 <- dput(test_qst)
structure(list(Date = structure(c(1588809600, 1588809600, 1588809600, 
1588809600, 1588809600, 1588809600, 1588809600, 1588809600, 1588809600, 
1588809600, 1588809600, 1588809600, 1588809600, 1588809600), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Depth = c(1.72, 3.07, 3.65, 4.58, 
5.39, 6.31, 7.27, 8.57, 9.73, 10.78, 11.71, 12.81, 13.79, 14.96
), salinity = c(34.7299999999999, 34.79, 34.76, 34.78, 34.77, 
34.79, 34.76, 34.71, 34.78, 34.78, 34.7999999999999, 34.86, 34.7999999999999, 
34.83)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-14L))

I created these lines (function) to modify a specific column of a data frame, I want to use this function to run it for different column and data frame, but the function does not work, I got a error code message.

change.date <-  function(df_date,col_nb,first.year, second.year){
  df_date$col_nb <- gsub(first.year, second.year,  df_date$col_nb)
  df_date$col_nb <- as.Date(df_date$col_nb)
  df_date$col_nb <-  as.numeric(df_date$col_nb)
    
}

change.date(df_2020,df_2020[1], "2020","2020")

Error in 
lt;-.data.frame`(*tmp*`, "col_nb", value = character(0)):
replacement table has 0 rows, replaced table has 7265

my reproducible data are:

df_2020 <- dput(test_qst)
structure(list(Date = structure(c(1588809600, 1588809600, 1588809600, 
1588809600, 1588809600, 1588809600, 1588809600, 1588809600, 1588809600, 
1588809600, 1588809600, 1588809600, 1588809600, 1588809600), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Depth = c(1.72, 3.07, 3.65, 4.58, 
5.39, 6.31, 7.27, 8.57, 9.73, 10.78, 11.71, 12.81, 13.79, 14.96
), salinity = c(34.7299999999999, 34.79, 34.76, 34.78, 34.77, 
34.79, 34.76, 34.71, 34.78, 34.78, 34.7999999999999, 34.86, 34.7999999999999, 
34.83)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-14L))

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

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

发布评论

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

评论(2

尸血腥色 2025-02-14 17:20:57

您可以尝试

change.date <-  function(df_date,col_nb,first.year, second.year){
  df_date[[col_nb]] <- gsub(first.year, second.year,  df_date[[col_nb]])
  
   
  df_date[[col_nb]] <- as.Date(df_date[[col_nb]])
  df_date[[col_nb]] <-  as.numeric(df_date[[col_nb]])
  df_date
}

change.date(df_2020, "Date", "2020","2020")

    Date Depth salinity
   <dbl> <dbl>    <dbl>
 1 18389  1.72     34.7
 2 18389  3.07     34.8
 3 18389  3.65     34.8
 4 18389  4.58     34.8
 5 18389  5.39     34.8
 6 18389  6.31     34.8
 7 18389  7.27     34.8
 8 18389  8.57     34.7
 9 18389  9.73     34.8
10 18389 10.8      34.8
11 18389 11.7      34.8
12 18389 12.8      34.9
13 18389 13.8      34.8
14 18389 15.0      34.8

You may try

change.date <-  function(df_date,col_nb,first.year, second.year){
  df_date[[col_nb]] <- gsub(first.year, second.year,  df_date[[col_nb]])
  
   
  df_date[[col_nb]] <- as.Date(df_date[[col_nb]])
  df_date[[col_nb]] <-  as.numeric(df_date[[col_nb]])
  df_date
}

change.date(df_2020, "Date", "2020","2020")

    Date Depth salinity
   <dbl> <dbl>    <dbl>
 1 18389  1.72     34.7
 2 18389  3.07     34.8
 3 18389  3.65     34.8
 4 18389  4.58     34.8
 5 18389  5.39     34.8
 6 18389  6.31     34.8
 7 18389  7.27     34.8
 8 18389  8.57     34.7
 9 18389  9.73     34.8
10 18389 10.8      34.8
11 18389 11.7      34.8
12 18389 12.8      34.9
13 18389 13.8      34.8
14 18389 15.0      34.8
掀纱窥君容 2025-02-14 17:20:57

使用GSUB时,您可能会发现一个问题是您丢失了日期。除非您需要数字时间尺度,否则最好保留绘图和分析日期。

使用dplyr,这会提取年份,更改它们,然后再次创建日期(即使它们是同年):

library(dplyr)

change.date <-  function(df_date, col_nb = "Date", first.year, second.year) {

  col_nb <- which(colnames(df_date) %in% col_nb)      
  
  df_date %>% 
    mutate(year = lubridate::year(.[[col_nb]])) %>% 
    mutate(year = ifelse(year == first.year, second.year, year)) %>% 
    mutate(Date = lubridate::make_date(year, lubridate::month(.[[col_nb]]), lubridate::day(.[[col_nb]]))) %>% 
    select(-year)
}

change.date(df_2020, "Date", 2020, 2020)

# A tibble: 14 x 3

   Date       Depth salinity
   <date>     <dbl>    <dbl>
 1 2020-05-07  1.72     34.7
 2 2020-05-07  3.07     34.8
 3 2020-05-07  3.65     34.8
 4 2020-05-07  4.58     34.8
 5 2020-05-07  5.39     34.8
 6 2020-05-07  6.31     34.8
 7 2020-05-07  7.27     34.8
 8 2020-05-07  8.57     34.7
 9 2020-05-07  9.73     34.8
10 2020-05-07 10.8      34.8
11 2020-05-07 11.7      34.8
12 2020-05-07 12.8      34.9
13 2020-05-07 13.8      34.8
14 2020-05-07 15.0      34.8

如果您确实想要数字日期,请使用此而不是第二行:

mutate(Date = as.numeric(lubridate::make_date(year, lubridate::month(.[[col_nb]]), lubridate::day(.[[col_nb]])))) %>% 

对您的功能的评论是在情况下保持一致。骆驼盒,蛇案例或较少的dot情况都是可以接受的,但是使用组合使跟踪变量更困难,例如df_date versus first.year

One issue you may find when using gsub is that you lose the dates. Unless you need a numerical timescale, then it may be better to keep dates for plotting and analysis.

Using dplyr, this extracts the years, changes them, and then creates dates again, (even if they are the same year):

library(dplyr)

change.date <-  function(df_date, col_nb = "Date", first.year, second.year) {

  col_nb <- which(colnames(df_date) %in% col_nb)      
  
  df_date %>% 
    mutate(year = lubridate::year(.[[col_nb]])) %>% 
    mutate(year = ifelse(year == first.year, second.year, year)) %>% 
    mutate(Date = lubridate::make_date(year, lubridate::month(.[[col_nb]]), lubridate::day(.[[col_nb]]))) %>% 
    select(-year)
}

change.date(df_2020, "Date", 2020, 2020)

# A tibble: 14 x 3

   Date       Depth salinity
   <date>     <dbl>    <dbl>
 1 2020-05-07  1.72     34.7
 2 2020-05-07  3.07     34.8
 3 2020-05-07  3.65     34.8
 4 2020-05-07  4.58     34.8
 5 2020-05-07  5.39     34.8
 6 2020-05-07  6.31     34.8
 7 2020-05-07  7.27     34.8
 8 2020-05-07  8.57     34.7
 9 2020-05-07  9.73     34.8
10 2020-05-07 10.8      34.8
11 2020-05-07 11.7      34.8
12 2020-05-07 12.8      34.9
13 2020-05-07 13.8      34.8
14 2020-05-07 15.0      34.8

If you do want numerical dates, then use this instead of the second last line:

mutate(Date = as.numeric(lubridate::make_date(year, lubridate::month(.[[col_nb]]), lubridate::day(.[[col_nb]])))) %>% 

One comment on your function is to be consistent on the case. Camel case, snake case or, less so, dot case are all acceptable, but using a combination makes it harder to keep track of variables, e.g. df_date versus first.year.

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