我想知道是否有一种简单的方法来减去形式奇数行甚至行。基本上:第一行(A1)减去第二行(A2),第三行(A3)减去第四行(A4)。
test <- structure(list(Well_positions = c("A1", "A2", "A3", "A4", "A5",
"A6", "A7", "A8", "A9", "A10", "A11", "A12"), Layout = c("SM1_1",
"BA1", "SM1_2", "BB1", "SM1_3", "BC1", "SM1_4", "BD1", "SM1_5",
"BE1", "ST1_1", "BF1"), Abs_18 = c(0.20585, 0.16226, 0.1695,
0.11268, 0.16271, 0.11269, 0.23633, 0.18636, 0.22289, 0.18856,
0.11974, 0.059685), FL_18 = c(3669, 51, 3578, 52, 3594, 51, 5378,
55, 5104, 54, 825, 58)), .Names = c("Well_positions", "Layout",
"Abs_18", "FL_18"), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
到目前为止,我只想创建两个单独的数据帧:
library(dplyr)
data_s <- filter(test, grepl("S", Layout))
data_b <-filter(test, grepl("B", Layout))
然后我想保留data_s
中的“Well_positions”和“Layout”,并获得其余列的差异(data_s[,3:4] - data_b[,3:4]
)。但是我不知道如何保留两个第一列...我能做的是从data_b中添加两列然后执行减法但是当我有更多列时它会变得非常混乱。
答案 0 :(得分:3)
result <- test[c(TRUE, FALSE),]
result$Abs_18 <- result$Abs_18 - test$Abs_18[c(FALSE,TRUE)]
result$FL_18 <- result$FL_18 - test$FL_18[c(FALSE,TRUE)]
这会将每个奇数行(1,3,5等)保存到新的数据帧中,这样您就可以保持布局和井位。然后,您只需从新数据框中的test
- 数据框的第3列和第4列的偶数行中减去值。
答案 1 :(得分:3)
以@ brettljausn的过滤解决方案为基础:
library(dplyr)
test %>% {cbind(.[c(T,F),] %>% rename_all(paste0,"_1"),
.[c(F,T),] %>% rename_all(paste0,"_2"))} %>%
mutate(delta_Abs_18 = Abs_18_1 - Abs_18_2,
delta_FL_18 = FL_18_1 - FL_18_2)
# Well_positions_1 Layout_1 Abs_18_1 FL_18_1 Well_positions_2 Layout_2 Abs_18_2 FL_18_2 delta_Abs_18 delta_FL_18
# 1 A1 SM1_1 0.20585 3669 A2 BA1 0.162260 51 0.043590 3618
# 2 A3 SM1_2 0.16950 3578 A4 BB1 0.112680 52 0.056820 3526
# 3 A5 SM1_3 0.16271 3594 A6 BC1 0.112690 51 0.050020 3543
# 4 A7 SM1_4 0.23633 5378 A8 BD1 0.186360 55 0.049970 5323
# 5 A9 SM1_5 0.22289 5104 A10 BE1 0.188560 54 0.034330 5050
# 6 A11 ST1_1 0.11974 825 A12 BF1 0.059685 58 0.060055 767
答案 2 :(得分:2)
使用library(dplyr)
test2 <- test %>%
group_by(grp = (row_number() - 1) %/% 2) %>%
summarise_all(funs(ifelse(is.numeric(.), first(.) - last(.), first(.)))) %>%
ungroup() %>%
select(-grp)
test2
# A tibble: 6 x 4
Well_positions Layout Abs_18 FL_18
<chr> <chr> <dbl> <dbl>
1 A1 SM1_1 0.043590 3618
2 A3 SM1_2 0.056820 3526
3 A5 SM1_3 0.050020 3543
4 A7 SM1_4 0.049970 5323
5 A9 SM1_5 0.034330 5050
6 A11 ST1_1 0.060055 767
的解决方案。
com.android.support:*
答案 3 :(得分:2)
dplyr
的另一种解决方案:
library(dplyr)
test %>%
mutate_all(funs(lead(.))) %>%
bind_cols(test, .) %>%
filter(rep(1:2, length.out = n()) == 1) %>%
mutate(diff_Abs = Abs_18 - Abs_181,
diff_FL = FL_18 - FL_181)
<强>结果:强>
Well_positions Layout Abs_18 FL_18 Well_positions1 Layout1 Abs_181 FL_181 diff_Abs
1 A1 SM1_1 0.20585 3669 A2 BA1 0.162260 51 0.043590
2 A3 SM1_2 0.16950 3578 A4 BB1 0.112680 52 0.056820
3 A5 SM1_3 0.16271 3594 A6 BC1 0.112690 51 0.050020
4 A7 SM1_4 0.23633 5378 A8 BD1 0.186360 55 0.049970
5 A9 SM1_5 0.22289 5104 A10 BE1 0.188560 54 0.034330
6 A11 ST1_1 0.11974 825 A12 BF1 0.059685 58 0.060055
diff_FL
1 3618
2 3526
3 3543
4 5323
5 5050
6 767
仅保留所需的列:
test %>%
mutate_all(funs(lead(.))) %>%
bind_cols(test, .) %>%
filter(rep(1:2, length.out = n()) == 1) %>%
mutate(diff_Abs = Abs_18 - Abs_181,
diff_FL = FL_18 - FL_181) %>%
select(Well_positions, Layout, starts_with("diff"))
<强>结果:强>
# A tibble: 6 x 4
Well_positions Layout diff_Abs diff_FL
<chr> <chr> <dbl> <dbl>
1 A1 SM1_1 0.043590 3618
2 A3 SM1_2 0.056820 3526
3 A5 SM1_3 0.050020 3543
4 A7 SM1_4 0.049970 5323
5 A9 SM1_5 0.034330 5050
6 A11 ST1_1 0.060055 767
答案 4 :(得分:2)
考虑奇数和偶数行条件:)
if (dim(test)[1]%%2!=0){
group=c(rep(1:((dim(test)[1]-1)/2), each=2),(dim(test)[1]-1)/2+1)}else{
group=rep(1:((dim(test)[1])/2), each=2)
}
test$group=group
t1=test%>%group_by(group)%>%dplyr::summarise(Abs18=ifelse(n()==1,`Abs 18`,diff(`Abs 18`)),FL18=ifelse(n()==1,`FL 18`,diff(`FL 18`)))
t2=test%>%group_by(group)%>%dplyr::slice(1)
t2[,c('Abs 18','FL 18')]=abs(t1[,2:3])
t2
# A tibble: 6 x 5
# Groups: group [6]
`Well positions` Layout `Abs 18` `FL 18` group
<chr> <chr> <dbl> <dbl> <dbl>
1 A1 SM1_1 0.04359 3618 1
2 A3 SM1_2 0.05682 3526 2
3 A5 SM1_3 0.05002 3543 3
4 A7 SM1_4 0.04997 5323 4
5 A9 SM1_5 0.03433 5050 5
6 A11 ST1_1 0.11974 825 6