R 行的条件操作和汇总
大家好 我有以下简化的数据框R 行的条件操作和汇总,r,R,大家好 我有以下简化的数据框 obj <- data.frame (id = c(1, 1, 2, 2, 3, 3, 4, 4), yr = c(1990, 1991, 1990, 1991, 1990, 1991, 1990, 1991), degree = c(1, 1, 1, 2, 1, 1, 0, 0) ) 非常感谢 带有dplyr: library(dplyr) obj %>% group_by(id) %>% mutate(chan
obj <- data.frame (id = c(1, 1, 2, 2, 3, 3, 4, 4),
yr = c(1990, 1991, 1990, 1991, 1990, 1991, 1990, 1991),
degree = c(1, 1, 1, 2, 1, 1, 0, 0)
)
非常感谢 带有
dplyr
:
library(dplyr)
obj %>% group_by(id) %>%
mutate(change = lead(degree)-degree) %>%
summarize(value = first(change), firstdeg = first(degree)) %>%
filter(firstdeg == 1) %>% select(id,value) %>%
ungroup
# A tibble: 3 x 2
id value
<dbl> <dbl>
1 1 0
2 2 1
3 3 0
库(dplyr)
obj%%>%分组依据(id)%%>%
突变(变化=铅(度)-度%>%
汇总(值=第一个(更改),第一个度=第一个(度))%>%
过滤器(firstdeg==1)%%>%选择(id,值)%%>%
解组
#一个tibble:3x2
id值
1 1 0
2 2 1
3 3 0
您可以在下列情况下使用case\u:
obj1 <- obj %>%
group_by(id) %>%
mutate(value = case_when(first(degree)==1 & last(degree)==1 ~ "0",
first(degree)==1 & last(degree)==2 ~"1",
TRUE ~ as.character("do not list"))
) %>%
distinct(id, value)
obj1%
分组依据(id)%>%
当(第一个(度)=1和最后一个(度)=1~“0”,
第一(度)==1和最后(度)==2~“1”,
TRUE~as.character(“不列出”))
) %>%
不同(id、值)
输出:
id value
<dbl> <chr>
1 1 0
2 2 1
3 3 0
4 4 do not list
id值
1 1 0
2 2 1
3 3 0
4.不要列出
这项工作:
# create dataset to store results:
results <- data.frame(id=unique(obj$id),
index=as.numeric(rep("NA", length(unique(obj$id))))
)
# fill results$index:
for(i in 1:length(unique(obj$id))){
temp.df <- subset(obj, id==unique(obj$id)[i])
if(length(unique(temp.df$degree))==1 & unique(temp.df$degree)==1) {results$index[i] <- 0} else
{if(temp.df$degree[1]==1 & temp.df$degree[2]==2) {results$index[i] <- 1} else
{results$index[i] <- "do not list"}}
}
results
#创建数据集以存储结果:
结果使用数据表
library(data.table)
setDT(obj)[, change := shift(degree, type = 'lead') - degree,
id][, .(value = first(change), firstdeg = first(degree)),
id][firstdeg == 1, .(id, value)]
您还可以使用以下解决方案:
library(dplyr)
obj %>%
group_by(id) %>%
summarise(value = ifelse(first(degree) == 1 & nth(degree, 2) == 2, "1",
ifelse(all(degree == 1), "0", "do not list")))
# A tibble: 4 x 2
id value
<dbl> <chr>
1 1 0
2 2 1
3 3 0
4 4 do not list
库(dplyr)
obj%>%
分组依据(id)%>%
总结(值=ifelse)(第一(度)==1和第四(度,2)==2,“1”,
ifelse(全部(度==1),“0”,“不列出”))
#一个tibble:4x2
id值
1 1 0
2 2 1
3 3 0
4.不要列出
很好,我的朋友
library(dplyr)
obj %>%
group_by(id) %>%
summarise(value = ifelse(first(degree) == 1 & nth(degree, 2) == 2, "1",
ifelse(all(degree == 1), "0", "do not list")))
# A tibble: 4 x 2
id value
<dbl> <chr>
1 1 0
2 2 1
3 3 0
4 4 do not list