Storing third value of given column in new column for each group in R -


i have dataset looks this:

user.id iso_date 1   3   2014-05-02 2   3   2014-05-05 3   3   2014-05-06 4   3   2014-05-20 5   3   2014-05-21 6   3   2014-05-24 7   3   2014-06-09 8   3   2014-06-14 9   3   2014-06-18 10  3   2014-06-26 11  3   2014-07-11 12  3   2014-07-21 13  3   2014-07-22 14  3   2014-07-25 15  3   2014-07-27 16  3   2014-08-03 17  3   2014-08-07 18  3   2014-08-12 19  3   2014-08-13 20  3   2014-08-16 21  3   2014-08-17 22  3   2014-08-20 23  3   2014-08-22 24  3   2014-08-31 25  3   2014-10-22 26  3   2014-11-19 27  3   2014-11-20 28  3   2014-11-23 29  3   2014-11-25 30  3   2014-12-06 31  3   2014-12-09 32  3   2014-12-10 33  3   2014-12-12 34  3   2014-12-14 35  3   2014-12-14 36  3   2014-12-14 37  3   2014-12-15 38  3   2014-12-16 39  3   2014-12-17 40  3   2014-12-18 41  3   2014-12-20 42  3   2015-01-08 43  3   2015-01-09 44  3   2015-01-11 45  3   2015-01-12 46  3   2015-01-14 47  3   2015-01-15 48  3   2015-01-18 49  3   2015-01-18 50  3   2015-01-19 51  3   2015-01-21 52  3   2015-01-22 53  3   2015-01-22 54  3   2015-01-23 55  3   2015-01-26 56  3   2015-01-27 57  3   2015-01-28 58  3   2015-01-29 59  3   2015-01-30 60  3   2015-01-30 61  3   2015-02-01 62  3   2015-02-02 63  3   2015-02-03 64  3   2015-02-04 65  3   2015-02-08 66  3   2015-02-09 67  3   2015-02-10 68  3   2015-02-13 69  3   2015-02-15 70  3   2015-02-16 71  3   2015-02-19 72  3   2015-02-20 73  3   2015-02-21 74  3   2015-02-23 75  3   2015-02-26 76  3   2015-02-28 77  3   2015-03-01 78  3   2015-03-11 79  3   2015-03-18 80  3   2015-03-22 81  3   2015-03-28 82  3   2015-04-03 83  3   2015-04-07 84  3   2015-04-08 85  3   2015-04-08 86  3   2015-04-15 87  3   2015-04-19 88  3   2015-04-21 89  3   2015-04-22 90  3   2015-04-24 91  3   2015-04-28 92  3   2015-05-03 93  3   2015-05-03 94  3   2015-05-04 95  3   2015-05-06 96  3   2015-05-08 97  3   2015-05-15 98  3   2015-05-16 99  3   2015-05-16 100 3   2015-05-19 101 3   2015-05-21 102 3   2015-05-21 103 3   2015-05-22 104 5   2015-02-05 105 7   2015-01-02 106 7   2015-01-03 107 7   2015-01-25 108 7   2015-02-21 109 7   2015-02-28 110 7   2015-03-02 111 7   2015-03-02 112 7   2015-03-07 113 7   2015-03-14 114 7   2015-05-01 115 9   2014-03-12 116 9   2014-03-12 117 9   2014-03-19 118 9   2014-04-10 119 9   2014-04-10 120 9   2014-04-10 121 9   2014-04-11 122 9   2014-05-30 123 9   2014-05-30 124 9   2014-06-06 125 9   2014-06-07 126 9   2014-06-14 127 9   2014-10-17 128 9   2014-10-17 129 9   2014-10-17 130 9   2014-10-17 131 9   2014-10-17 132 9   2014-10-17 133 9   2014-10-17 134 9   2014-10-19 135 9   2014-10-20 136 9   2014-10-20 137 9   2014-12-20 138 13  2014-07-08 139 13  2014-07-08 140 13  2014-07-08 141 13  2014-07-11 142 13  2014-07-11 143 13  2014-07-18 144 13  2014-07-19 145 13  2014-07-23 146 13  2014-07-23 147 13  2014-07-27 148 13  2014-07-29 149 13  2014-07-31 150 13  2014-08-02 151 13  2014-08-03 152 13  2014-08-06 153 13  2014-08-14 154 13  2014-08-14 155 13  2014-08-18 156 13  2014-08-19 157 13  2014-08-26 158 13  2014-08-30 159 13  2014-09-02 160 13  2014-09-10 161 13  2014-09-12 162 13  2014-09-13 163 13  2014-09-18 164 13  2014-09-20 165 13  2014-09-21 166 13  2014-09-24 167 13  2014-09-28 168 13  2014-09-30 169 13  2014-10-04 170 13  2014-10-09 171 13  2014-10-15 172 13  2014-10-20 173 13  2014-10-20 174 13  2014-10-20 175 13  2014-10-20 176 13  2014-10-25 177 13  2014-10-26 178 13  2014-10-29 179 13  2014-11-10 180 13  2014-11-28 181 13  2014-11-28 182 13  2014-11-28 183 13  2014-11-28 184 13  2014-11-29 185 13  2014-12-03 186 13  2014-12-05 187 13  2014-12-05 188 13  2014-12-10 189 13  2015-01-03 190 13  2015-03-08 191 13  2015-03-22 192 13  2015-04-06 193 13  2015-04-16 194 13  2015-04-21 195 13  2015-04-22 196 13  2015-04-26 197 13  2015-05-05 198 13  2015-05-07 199 13  2015-05-15 200 13  2015-05-21 201 16  2014-03-11 202 16  2014-03-13 203 16  2014-03-15 204 16  2014-04-12 205 16  2014-04-14 206 16  2014-04-23 207 16  2014-05-26 208 16  2014-05-30 209 16  2014-05-31 210 16  2014-06-10 211 16  2014-06-26 212 16  2014-08-18 213 16  2014-08-21 214 16  2014-08-24 215 16  2014-08-29 216 16  2014-09-01 217 16  2014-09-07 218 16  2014-09-15 219 16  2014-09-17 220 16  2014-09-24 221 16  2014-09-29 222 16  2014-10-06 223 16  2014-10-07 224 16  2014-10-08 225 16  2014-10-20 226 16  2014-10-20 227 16  2014-10-20 228 16  2014-11-12 229 16  2014-11-12 

i want create 2 new columns store 3rd , 6th value of iso_date each user.id separately.

i tried this:

users <- users %>% arrange(user.id) %>% group_by(user.id) %>% mutate(third_date = head(iso_date, 3)) %>% mutate(fifth_date = head(iso_date, 6)) 

but not helping. there way in r?

you can convert 'iso_date' column 'date' class (if not), group_by 'user.id', arrange 'iso_date' , create new columns 3rd , 6th observation of 'iso_date'

library(dplyr) users1 <- users %>%             mutate(iso_date = as.date(iso_date)) %>%              group_by(user.id) %>%              arrange(iso_date) %>%              mutate(third_date = iso_date[3l], sixth_date=iso_date[6l]) 

or using data.table

library(data.table) setdt(users)[, iso_date:= as.date(iso_date)            ][order(iso_date),                 c('third_date', 'sixth_date') := list(iso_date[3l], iso_date[6l]) ,             by= user.id] 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -