create account

R: How to transform info like 61 years 02 months to total number of months? by snippets

View this thread on: hive.blogpeakd.comecency.com
· @snippets ·
$0.24
R: How to transform info like 61 years 02 months to total number of months?
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"))
```

![Screenshot 2023-10-14 at 9.31.49 AM.png](https://files.peakd.com/file/peakd-hive/snippets/23tSwpFzx1xMUbaL6atiDwaab2SZZAmuhzhVJNW3XVXMkk2WisGXgdUHzt7uREDWnCAqQ.png)

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'.

![Screenshot 2023-10-14 at 9.31.02 AM.png](https://files.peakd.com/file/peakd-hive/snippets/23tSyukajs3FSyzsvxCDVvpL7gFUAN84vZChftu9igdUVAcJnYkVaKEfN5Qz7N9p2MrQG.png)


![snippets.png](https://files.peakd.com/file/peakd-hive/snippets/AKNMuVqPrNWuLjdyxwLdzm99obv1dcnZWufeJdBoWwecd9UBvGxERepophy4Epu.png)
👍  , , , , , , , , , , , , , , , , ,
properties (23)
authorsnippets
permlinkr-how-to-transform-info-like-61-years-02-months-to-total-number-of-months
categoryhive-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"]}"
created2023-10-14 01:42:09
last_update2023-10-14 01:42:09
depth0
children0
last_payout2023-10-21 01:42:09
cashout_time1969-12-31 23:59:59
total_payout_value0.122 HBD
curator_payout_value0.119 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,645
author_reputation801,725,525,485
root_title"R: How to transform info like 61 years 02 months to total number of months? "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id127,972,496
net_rshares589,508,848,893
author_curate_reward""
vote details (18)