Search code examples
rdplyrtidyverse

How to change values from a colunm based on another columns` values in R


I have a dataframe with a ".row" colunm, whose values i need to change. The ".row" colunm has multiple entries, ranging from 1 to 215. I would like to change the numeric value (1 to 215) into a date value, like: 1==jan/2003 and 215==dez/21.

I might not have been specific enough. The ".row" colunm has 1334 observations, with values in the 1 to 215 range. Each value corresponds to a given month, with 1 being feb/2004 and 215 being dez/2021. How could i operate this change, that is, one that would transform this colunm value`s into the respective dates

Can anyone help please

  resamples_extracted$.row
   [1] 204 205 206 207 208 209 210 211 212 213 214 215 192 193 194 195 196 197 198 199 200 201 202 203 180 181 182 183 184 185 186 187 188 189 190 191 168 169 170 171
  [41] 172 173 174 175 176 177 178 179 156 157 158 159 160 161 162 163 164 165 166 167 144 145 146 147 148 149 150 151 152 153 154 155 132 133 134 135 136 137 138 139
  [81] 140 141 142 143 120 121 122 123 124 125 126 127 128 129 130 131 108 109 110 111 112 113 114 115 116 117 118 119  96  97  98  99 100 101 102 103 104 105 106 107
 [121]  84  85  86  87  88  89  90  91  92  93  94  95  72  73  74  75  76  77  78  79  80  81  82  83  60  61  62  63  64  65  66  67  68  69  70  71  48  49  50  51
 [161]  52  53  54  55  56  57  58  59  36  37  38  39  40  41  42  43  44  45  46  47  24  25  26  27  28  29  30  31  32  33  34  35 204 205 206 207 208 209 210 211
 [201] 212 213 214 215 192 193 194 195 196 197 198 199 200 201 202 203 180 181 182 183 184 185 186 187 188 189 190 191 168 169 170 171 172 173 174 175 176 177 178 179
 [241] 156 157 158 159 160 161 162 163 164 165 166 167 144 145 146 147 148 149 150 151 152 153 154 155 132 133 134 135 136 137 138 139 140 141 142 143 120 121 122 123
 [281] 124 125 126 127 128 129 130 131 108 109 110 111 112 113 114 115 116 117 118 119  96  97  98  99 100 101 102 103 104 105 106 107  84  85  86  87  88  89  90  91
 [321]  92  93  94  95  72  73  74  75  76  77  78  79  80  81  82  83  60  61  62  63  64  65  66  67  68  69  70  71  48  49  50  51  52  53  54  55  56  57  58  59
 [361]  36  37  38  39  40  41  42  43  44  45  46  47  24  25  26  27  28  29  30  31  32  33  34  35 204 205 206 207 208 209 210 211 212 213 214 215 192 193 194 195
 [401] 196 197 198 199 200 201 202 203 180 181 182 183 184 185 186 187 188 189 190 191 168 169 170 171 172 173 174 175 176 177 178 179 156 157 158 159 160 161 162 163
 [441] 164 165 166 167 144 145 146 147 148 149 150 151 152 153 154 155 132 133 134 135 136 137 138 139 140 141 142 143 120 121 122 123 124 125 126 127 128 129 130 131
 [481] 108 109 110 111 112 113 114 115 116 117 118 119  96  97  98  99 100 101 102 103 104 105 106 107  84  85  86  87  88  89  90  91  92  93  94  95  72  73  74  75
 [521]  76  77  78  79  80  81  82  83  60  61  62  63  64  65  66  67  68  69  70  71  48  49  50  51  52  53  54  55  56  57  58  59  36  37  38  39  40  41  42  43
 [561]  44  45  46  47  24  25  26  27  28  29  30  31  32  33  34  35 204 205 206 207 208 209 210 211 212 213 214 215 192 193 194 195 196 197 198 199 200 201 202 203
 [601] 180 181 182 183 184 185 186 187 188 189 190 191 168 169 170 171 172 173 174 175 176 177 178 179 156 157 158 159 160 161 162 163 164 165 166 167 144 145 146 147
 [641] 148 149 150 151 152 153 154 155 132 133 134 135 136 137 138 139 140 141 142 143 120 121 122 123 124 125 126 127 128 129 130 131 108 109 110 111 112 113 114 115
 [681] 116 117 118 119  96  97  98  99 100 101 102 103 104 105 106 107  84  85  86  87  88  89  90  91  92  93  94  95  72  73  74  75  76  77  78  79  80  81  82  83
 [721]  60  61  62  63  64  65  66  67  68  69  70  71  48  49  50  51  52  53  54  55  56  57  58  59  36  37  38  39  40  41  42  43  44  45  46  47  24  25  26  27
 [761]  28  29  30  31  32  33  34  35 204 205 206 207 208 209 210 211 212 213 214 215 192 193 194 195 196 197 198 199 200 201 202 203 180 181 182 183 184 185 186 187
 [801] 188 189 190 191 168 169 170 171 172 173 174 175 176 177 178 179 156 157 158 159 160 161 162 163 164 165 166 167 144 145 146 147 148 149 150 151 152 153 154 155
 [841] 132 133 134 135 136 137 138 139 140 141 142 143 120 121 122 123 124 125 126 127 128 129 130 131 108 109 110 111 112 113 114 115 116 117 118 119  96  97  98  99
 [881] 100 101 102 103 104 105 106 107  84  85  86  87  88  89  90  91  92  93  94  95  72  73  74  75  76  77  78  79  80  81  82  83  60  61  62  63  64  65  66  67
 [921]  68  69  70  71  48  49  50  51  52  53  54  55  56  57  58  59  36  37  38  39  40  41  42  43  44  45  46  47  24  25  26  27  28  29  30  31  32  33  34  35
 [961] 204 205 206 207 208 209 210 211 212 213 214 215 192 193 194 195 196 197 198 199 200 201 202 203 180 181 182 183 184 185 186 187 188 189 190 191 168 169 170 171
 [ reached getOption("max.print") -- omitted 344 entries ]

I would like to change all this numbers into specific dates. 1 would be the start, say 01-01-2003. 215 would be the latest entry, say 01-12-2021


Solution

  • It sounds like the .row column contains the number of months since January 2003 and you want to convert the number of months to a date.

    Based on this question you can do this using the lubridate package. Something like the following:

    library(dplyr)
    library(lubridate)
    
    df = df %>%
      mutate(month_date = as.Date('2003-01-01') %m+% months(.row))
    

    Lubridate documentation here.