This is the data frame I originally have, the values in t_p and t_c are ages.
I want to make the id repeat the number of times from the start of data collecion (which is age 50), so I need to take t_c age (ignore t_p) and subtract by 50, and that value is the number of times I want a person’s id number to repeat, but I am unsure how to do that.
This is a rough structure of how I want to format my data frame.
I want it to show the exact age
I am attempting to make two data frames and bind them but I cannot figure out the repeating id number aspect!
You can try:
result = reframe(df, age=50:floor(t_c),.by = id)
or
library(data.table)
result = setDT(df)[rep(id,floor(t_c)-50)][, .(age=0:.N+50),id]
I’m not sure what you want in the final column. If you want all NA except the last row per id, then you can add this:
mutate(result, t_c = if_else(row_number()==n(),age,NA),.by = id)
or
result[, t_c:=fifelse(age==max(age),age,NA),id]
2
base R
would look like this
df <- data.frame(
id = 1:5,
t_c = c(75.41888, 90.89741, 55.75988, 94.59065, 94.2281)
)
df <- cbind(
df[rep(1:nrow(df), floor(df$t_c)-50 + 1),],
age = unlist(sapply(floor(df$t_c), seq, from = 50))
)
2
Edit:
An even easier way:
df %>%
group_by(id) %>%
reframe(t_c = 50:floor(t_c))
The easiest way I can think of doing this involves the tidyr’s uncount. Simply subtract the age by the 50 and add 1 for an additional row for the age 50 itself to get the number of rows we want for each id. Then, you can use uncount to create those rows and a numeric id within each group. Reverse the process to get the ages.
This way you wouldn’t lose any other columns:
library(tidyverse)
df <- data.frame(
id = 1:5,
t_c = c(75.41888, 90.89741, 55.75988, 94.59065, 94.2281)
)
df %>%
mutate(count = floor(t_c - 49)) %>%
uncount(count, .id = "t_c") %>%
mutate(t_c = 49 + t_c)
#> id t_c
#> 1 1 50
#> 2 1 51
#> 3 1 52
#> 4 1 53
#> 5 1 54
#> 6 1 55
#> 7 1 56
#> 8 1 57
#> 9 1 58
#> 10 1 59
#> 11 1 60
#> 12 1 61
#> 13 1 62
#> 14 1 63
#> 15 1 64
#> 16 1 65
#> 17 1 66
#> 18 1 67
#> 19 1 68
#> 20 1 69
#> 21 1 70
#> 22 1 71
#> 23 1 72
#> 24 1 73
#> 25 1 74
#> 26 1 75
#> 27 2 50
#> 28 2 51
#> 29 2 52
#> 30 2 53
#> 31 2 54
#> 32 2 55
#> 33 2 56
#> 34 2 57
#> 35 2 58
#> 36 2 59
#> 37 2 60
#> 38 2 61
#> 39 2 62
#> 40 2 63
#> 41 2 64
#> 42 2 65
#> 43 2 66
#> 44 2 67
#> 45 2 68
#> 46 2 69
#> 47 2 70
#> 48 2 71
#> 49 2 72
#> 50 2 73
#> 51 2 74
#> 52 2 75
#> 53 2 76
#> 54 2 77
#> 55 2 78
#> 56 2 79
#> 57 2 80
#> 58 2 81
#> 59 2 82
#> 60 2 83
#> 61 2 84
#> 62 2 85
#> 63 2 86
#> 64 2 87
#> 65 2 88
#> 66 2 89
#> 67 2 90
#> 68 3 50
#> 69 3 51
#> 70 3 52
#> 71 3 53
#> 72 3 54
#> 73 3 55
#> 74 4 50
#> 75 4 51
#> 76 4 52
#> 77 4 53
#> 78 4 54
#> 79 4 55
#> 80 4 56
#> 81 4 57
#> 82 4 58
#> 83 4 59
#> 84 4 60
#> 85 4 61
#> 86 4 62
#> 87 4 63
#> 88 4 64
#> 89 4 65
#> 90 4 66
#> 91 4 67
#> 92 4 68
#> 93 4 69
#> 94 4 70
#> 95 4 71
#> 96 4 72
#> 97 4 73
#> 98 4 74
#> 99 4 75
#> 100 4 76
#> 101 4 77
#> 102 4 78
#> 103 4 79
#> 104 4 80
#> 105 4 81
#> 106 4 82
#> 107 4 83
#> 108 4 84
#> 109 4 85
#> 110 4 86
#> 111 4 87
#> 112 4 88
#> 113 4 89
#> 114 4 90
#> 115 4 91
#> 116 4 92
#> 117 4 93
#> 118 4 94
#> 119 5 50
#> 120 5 51
#> 121 5 52
#> 122 5 53
#> 123 5 54
#> 124 5 55
#> 125 5 56
#> 126 5 57
#> 127 5 58
#> 128 5 59
#> 129 5 60
#> 130 5 61
#> 131 5 62
#> 132 5 63
#> 133 5 64
#> 134 5 65
#> 135 5 66
#> 136 5 67
#> 137 5 68
#> 138 5 69
#> 139 5 70
#> 140 5 71
#> 141 5 72
#> 142 5 73
#> 143 5 74
#> 144 5 75
#> 145 5 76
#> 146 5 77
#> 147 5 78
#> 148 5 79
#> 149 5 80
#> 150 5 81
#> 151 5 82
#> 152 5 83
#> 153 5 84
#> 154 5 85
#> 155 5 86
#> 156 5 87
#> 157 5 88
#> 158 5 89
#> 159 5 90
#> 160 5 91
#> 161 5 92
#> 162 5 93
#> 163 5 94
Created on 2024-07-14 with reprex v2.1.1