我有这种情况:
Animal Date.1 Weight.1 Date.2 Weight.2 Date.3 Weight.3 Date.4 Weight.4
1 12/18/19 55 1/2/20 67 6/6/20 101
2 12/18/19 64 1/3/20 69 2/4/20 80
3 12/18/19 75
4 1/3/20 85
5 12/18/19 88 1/6/20 86 2/7/20 96 6/6/20 100
我想选择weight.1之后的最后一个重量,如下所示:
Animal Date.last Last Weight
1 6/6/20 101
2 2/4/20 80
3 NA NA
4 1/3/20 85
5 6/6/20 100
对不起,我没有显示任何脚本,但我什至不知道从哪里开始。
答案 0 :(得分:1)
在重塑为“长”格式后,这里是一个选项
TopAppBar(
title = {
Stack(modifier = Modifier.None) {
Align(alignment = Alignment.Center) {
Text(
text = "MyApp",
style = (+themeTextStyle { h6 }).withOpacity(0.7f)
)
}
Align(alignment = Alignment.CenterRight) {
CircleImageButton(
resource = Res.drawable.ic_action_reload,
onCLick = onRefreshClick
)
}
}
},
color = Color.White
)
library(dplyr)
library(tidyr)
library(stringr)
df1 %>%
pivot_longer(cols = -Animal, names_to = c(".value", "group"),
names_sep="[.]", values_drop_na = TRUE) %>%
group_by(Animal) %>%
slice(n()) %>%
ungroup %>%
mutate_at(vars(Date, Weight), ~ replace(., group == 1, NA)) %>%
select(-group) %>%
rename_at(2:3, ~ str_c(., 'last'))
# A tibble: 5 x 3
# Animal Datelast Weightlast
# <int> <chr> <int>
#1 1 6/6/20 101
#2 2 2/4/20 80
#3 3 <NA> NA
#4 4 1/3/20 85
#5 5 6/6/20 100
答案 1 :(得分:1)
在基数R中,您可以使用aggregate
+ reshape
函数:
df1 <- reshape(`is.na<-`(df,df==""),2:ncol(df),idvar = "Animal",dir="long")
aggregate(cbind(Date,Weight)~Animal, df1,
function(x)if(is.na(x[2])) NA else as.character(x[max(which(!is.na(x)))]),
na.action = identity)
Animal Date Weight
1 1 6/6/20 101
2 2 2/4/20 80
3 3 <NA> <NA>
4 4 1/3/20 85
5 5 6/6/20 100