Error in `[[<-`(`*tmp*`, id_index, value = HD_before) : no such index at level 1; How can I solve this problem?

huangapple go评论69阅读模式
英文:

Error in `[[<-`(`*tmp*`, id_index, value = HD_before) : no such index at level 1; How can I solve this problem?

问题

When I enter the code in R as below

library(dplyr)
library(readxl)

fn <- function(data, id_variable_name)
{ 
list <- list()
ids <- unique(data[,id_variable_name]) 
for(id_index in ids){
 data_by_group <- data %>% filter(id == id_index)
 data_plus <- data_by_group %>% filter(timedif > 0)
 HD_before <- arrange(data_plus) %>% head(1)
 data_minus <- data_by_group %>% filter(timedif < 0)
 HD_after <- arrange(data_minus) %>% head(1)
 HD_before_after <- rbind(HD_before, HD_after)
 list[[id_index]] <- HD_before_after
 }
output <- do.call(rbind.data.frame, list)
return(output)
}

temp <- fn(data_temp, "id")

an error code

Error in `[[<-`(`*tmp*`, id_index, value = HD_before_after) : 
  no such index at level 1

appears.

What is wrong with my code and how can I fix it?

Details of data_temp is

 id timedif
1 -5850 
1 -4975
1 -7855
1 -8817
1 -5969
2 1500
2 1708
2 -1442
2 1749
3 -4110
3 -4050
3 -4140
3 -4094
4 -8651
4 -8576
4 -8561
4 -8555
4 -8546
4 -5621
4 -4901
4 -8728
4 -8548
4 -5693
4 -5513

Both id and timedif are numeric.

英文:

When I enter the code in R as below

library(dplyr)
library(readxl)

fn&lt;-function(data, id_variable_name)
{ 
list&lt;-list()
ids&lt;-unique(data[,id_variable_name]) 
for(id_index in ids){
 data_by_group&lt;-data %&gt;% filter(id==id_index)
 data_plus&lt;-data_by_group %&gt;% filter(timedif&gt;0)
 HD_before&lt;-arrange(data_plus) %&gt;% head(1)
 data_minus&lt;-data_by_group %&gt;% filter(timedif&lt;0)
 HD_after&lt;-arrange(data_minus) %&gt;% head(1)
 HD_before_after&lt;-rbind(HD_before, HD_after)
 list[[id_index]]&lt;-HD_before_after
 }
output&lt;-do.call(rbind.data.frame, list)
return(output)
}

temp&lt;-fn(data_temp, &quot;id&quot;)

an error code

Error in `[[&lt;-`(`*tmp*`, id_index, value = HD_before_after) : 
  no such index at level 1

appears.

What is wrong with my code and how can I fix it?

Details of data_temp is

 id timedif
1 -5850 
1 -4975
1 -7855
1 -8817
1 -5969
2 1500
2 1708
2 -1442
2 1749
3 -4110
3 -4050
3 -4140
3 -4094
4 -8651
4 -8576
4 -8561
4 -8555
4 -8546
4 -5621
4 -4901
4 -8728
4 -8548
4 -5693
4 -5513

Both id and timedif are numeric.

答案1

得分: 1

以下是您要翻译的内容:

"Perhaps a simpler approach:

arrange(data_temp) %&gt;%
  group_by(id, g = timedif &gt; 0) %&gt;%
  slice_head(n = 1) %&gt;%
  ungroup() %&gt;%
  select(-g)
# # A tibble: 5 &#215; 2
#      id timedif
#   &lt;int&gt;   &lt;int&gt;
# 1     1   -5850
# 2     2   -1442
# 3     2    1500
# 4     3   -4110
# 5     4   -8651

Functionized as:

library(rlang) # enquo
fn &lt;- function(data, id_variable_name, time) {
  arrange(data) %&gt;%
    group_by(!!enquo(id_variable_name), g = !!enquo(time) &gt; 0) %&gt;%
    slice_head(n = 1) %&gt;%
    ungroup() %&gt;%
    select(-g)
}
fn(data_temp, id, timedif)
# # A tibble: 5 &#215; 2
#      id timedif
#   &lt;int&gt;   &lt;int&gt;
# 1     1   -5850
# 2     2   -1442
# 3     2    1500
# 4     3   -4110
# 5     4   -8651

Notice that this is using non-standard evaluation (NSE) for identifying the column names, and that I added time=."

英文:

Perhaps a simpler approach:

arrange(data_temp) %&gt;%
  group_by(id, g = timedif &gt; 0) %&gt;%
  slice_head(n = 1) %&gt;%
  ungroup() %&gt;%
  select(-g)
# # A tibble: 5 &#215; 2
#      id timedif
#   &lt;int&gt;   &lt;int&gt;
# 1     1   -5850
# 2     2   -1442
# 3     2    1500
# 4     3   -4110
# 5     4   -8651

Functionized as:

library(rlang) # enquo
fn &lt;- function(data, id_variable_name, time) {
  arrange(data) %&gt;%
    group_by(!!enquo(id_variable_name), g = !!enquo(time) &gt; 0) %&gt;%
    slice_head(n = 1) %&gt;%
    ungroup() %&gt;%
    select(-g)
}
fn(data_temp, id, timedif)
# # A tibble: 5 &#215; 2
#      id timedif
#   &lt;int&gt;   &lt;int&gt;
# 1     1   -5850
# 2     2   -1442
# 3     2    1500
# 4     3   -4110
# 5     4   -8651

Notice that this is using non-standard evaluation (NSE) for identifying the column names, and that I added time=.

huangapple
  • 本文由 发表于 2023年4月17日 16:32:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定