I have a task where I need to transform info like 61 years 02 months into total number of months in a dataframe column. To do this, I use the following libraries: ``` library(dplyr) library(stringr) ``` To see a sample data frame, you can run this code. ``` df <- data.frame(date_str = c("61 years 04 months", "10 years", "7 years 09 months")) ```  The following code will create a new column with the total number of months. ``` df <- df %>% mutate(years = as.numeric(str_extract(date_str, "\\d+(?= years)")), months = as.numeric(str_extract(date_str, "\\d+(?= months)")), years = ifelse(is.na(years), 0, years), # Set NAs to 0 for years months = ifelse(is.na(months), 0, months), # Set NAs to 0 for months total_months = years*12 + months) %>% select(-years, -months) # Remove intermediate columns if you don't need them print(df) ``` The magic is with the **str_extract(date_str, "\\d+(?= years)"** where the year value is extracted. To cater to cases where there is 10 years and no months, the following is used: **ifelse(is.na(months), 0, months)**. This derives the total months: 'total_months = years*12 + months'.  
author | snippets |
---|---|
permlink | r-how-to-transform-info-like-61-years-02-months-to-total-number-of-months |
category | hive-138200 |
json_metadata | "{"app":"peakd/2023.10.1","format":"markdown","description":"The magic is with the str_extract(date_str, \"\\\\d+(?= years)\" where the year value is extracted...","tags":["coding","programming","development","proofofbrain","stemgeeks","datascience","rstats"],"users":[],"image":["https://files.peakd.com/file/peakd-hive/snippets/AKNMuVqPrNWuLjdyxwLdzm99obv1dcnZWufeJdBoWwecd9UBvGxERepophy4Epu.png","https://files.peakd.com/file/peakd-hive/snippets/23tSwpFzx1xMUbaL6atiDwaab2SZZAmuhzhVJNW3XVXMkk2WisGXgdUHzt7uREDWnCAqQ.png","https://files.peakd.com/file/peakd-hive/snippets/23tSyukajs3FSyzsvxCDVvpL7gFUAN84vZChftu9igdUVAcJnYkVaKEfN5Qz7N9p2MrQG.png"]}" |
created | 2023-10-14 01:42:09 |
last_update | 2023-10-14 01:42:09 |
depth | 0 |
children | 0 |
last_payout | 2023-10-21 01:42:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.122 HBD |
curator_payout_value | 0.119 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,645 |
author_reputation | 801,725,525,485 |
root_title | "R: How to transform info like 61 years 02 months to total number of months? " |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 0 |
post_id | 127,972,496 |
net_rshares | 589,508,848,893 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
slider2990 | 0 | 6,347,023,110 | 100% | ||
joeyarnoldvn | 0 | 476,671,183 | 1.47% | ||
davidesimoncini | 0 | 377,805,866 | 100% | ||
babytarazkp | 0 | 3,577,645,977 | 40% | ||
abh12345.stem | 0 | 1,278,175,436 | 100% | ||
elianaicgomes | 0 | 1,867,906,277 | 5% | ||
dorkpower | 0 | 3,635,316,062 | 100% | ||
sillybilly | 0 | 580,482,711 | 100% | ||
he-index | 0 | 3,072,836,073 | 10% | ||
meestemboom | 0 | 1,819,227,193 | 100% | ||
krishu.stem | 0 | 535,745,313 | 100% | ||
dronegirl | 0 | 267,553,369 | 100% | ||
juecoree.stem | 0 | 595,505,847 | 100% | ||
holovision.stem | 0 | 338,952,472 | 100% | ||
irivers | 0 | 145,511,532 | 100% | ||
cryptothesis | 0 | 261,785,030,973 | 100% | ||
mituoshizhe | 0 | 302,479,061,892 | 100% | ||
taimen | 0 | 328,397,607 | 60% |