Reputation: 279
Applying the logic as below:
constant = 5
if count <= constant, print rows from value 1[1:5], once count > constant, print rows from value 2[1:5].
not using indexing - slicing, need a logic.,
Expected output:
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
here in output column - first 1:5 rows are from value_1 and the remaining are from value_2 - 1:7
Upvotes: 1
Views: 664
Reputation: 16121
Here's an option using tidyverse
and some reshaping:
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
Upvotes: 1