应用如下逻辑:
常数= 5
如果count <= constant,则从值1 [1:5]打印行,一旦count> constant,则从值2 [1:5]打印行。
不使用索引-切片,需要逻辑。
预期产量:
count value_1 value_2 output
1 0.001138636 0.081404856 0.001138636
2 0.001157974 0.089056417 0.001157974
3 0.00117294 0.098103887 0.00117294
4 0.00124517 0.109297111 0.00124517
5 0.001369958 0.123153932 0.001369958
6 0.001494746 0.141047465 0.081404856
7 0.001619535 0.165075631 0.089056417
8 0.001744323 0.198308568 0.098103887
9 0.001771541 0.248464171 0.109297111
10 0.001713549 0.331921807 0.123153932
11 0.001592526 0.001197517 0.141047465
12 0.001342363 0.00159737 0.165075631
在输出列中-前1:5行来自value_1,其余行来自value_2-1:7
这是一个使用选项tidyverse
和一些重塑的方法:
df = read.table(text = "
count value_1 value_2 output
1 0.001138636 0.081404856 0.001138636
2 0.001157974 0.089056417 0.001157974
3 0.00117294 0.098103887 0.00117294
4 0.00124517 0.109297111 0.00124517
5 0.001369958 0.123153932 0.001369958
6 0.001494746 0.141047465 0.081404856
7 0.001619535 0.165075631 0.089056417
8 0.001744323 0.198308568 0.098103887
9 0.001771541 0.248464171 0.109297111
10 0.001713549 0.331921807 0.123153932
11 0.001592526 0.001197517 0.141047465
12 0.001342363 0.00159737 0.165075631
", header=T)
df$output = NULL
library(tidyverse)
# input constant
constant = 5
# calculate rest of values needed for value_2
nn = nrow(df) - constant
df %>%
gather(x,y,-count) %>%
group_by(x) %>%
filter((x == "value_1" & row_number() <= constant) | (x == "value_2" & row_number() <= nn)) %>%
pull(y) -> df$output
df
# count value_1 value_2 output
# 1 1 0.001138636 0.081404856 0.001138636
# 2 2 0.001157974 0.089056417 0.001157974
# 3 3 0.001172940 0.098103887 0.001172940
# 4 4 0.001245170 0.109297111 0.001245170
# 5 5 0.001369958 0.123153932 0.001369958
# 6 6 0.001494746 0.141047465 0.081404856
# 7 7 0.001619535 0.165075631 0.089056417
# 8 8 0.001744323 0.198308568 0.098103887
# 9 9 0.001771541 0.248464171 0.109297111
# 10 10 0.001713549 0.331921807 0.123153932
# 11 11 0.001592526 0.001197517 0.141047465
# 12 12 0.001342363 0.001597370 0.165075631
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句