英文:
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<-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.
答案1
得分: 1
以下是您要翻译的内容:
"Perhaps a simpler approach:
arrange(data_temp) %>%
group_by(id, g = timedif > 0) %>%
slice_head(n = 1) %>%
ungroup() %>%
select(-g)
# # A tibble: 5 × 2
# id timedif
# <int> <int>
# 1 1 -5850
# 2 2 -1442
# 3 2 1500
# 4 3 -4110
# 5 4 -8651
Functionized as:
library(rlang) # enquo
fn <- function(data, id_variable_name, time) {
arrange(data) %>%
group_by(!!enquo(id_variable_name), g = !!enquo(time) > 0) %>%
slice_head(n = 1) %>%
ungroup() %>%
select(-g)
}
fn(data_temp, id, timedif)
# # A tibble: 5 × 2
# id timedif
# <int> <int>
# 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) %>%
group_by(id, g = timedif > 0) %>%
slice_head(n = 1) %>%
ungroup() %>%
select(-g)
# # A tibble: 5 × 2
# id timedif
# <int> <int>
# 1 1 -5850
# 2 2 -1442
# 3 2 1500
# 4 3 -4110
# 5 4 -8651
Functionized as:
library(rlang) # enquo
fn <- function(data, id_variable_name, time) {
arrange(data) %>%
group_by(!!enquo(id_variable_name), g = !!enquo(time) > 0) %>%
slice_head(n = 1) %>%
ungroup() %>%
select(-g)
}
fn(data_temp, id, timedif)
# # A tibble: 5 × 2
# id timedif
# <int> <int>
# 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=
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论