Home > Enterprise >  Apply mutate and store results in a nested tibble column in the data frame
Apply mutate and store results in a nested tibble column in the data frame

Time:08-31

I have some data which looks like:

# A tibble: 1 × 14
  purchasePrice mtsSqrd mtsSquaredBins price_per_mtsSquared_… operatingExpens… vacancy capRate growthRateInc growthRateExp generalPotentia…
          <dbl>   <dbl> <fct>                           <dbl>            <dbl>   <dbl>   <dbl>         <dbl>         <dbl>            <dbl>
1        419000     125 (120,130]                        19.9               10     0.1    0.08          0.03          0.02            2493.
# … with 4 more variables: generalVacancy <dbl>, effGrossRev <dbl>, operatingExpenses <dbl>, netOpInc <dbl>

I want to mutate the data and add columns in a nested tibble.

I want to do something like:

T = 4

  mutate(
    generalPotentialRev_T = generalPotentialRev*(1   growthRateInc)^(1:T)
  )

I can do it manually:

myData$generalPotentialRev * (1   myData$growthRateInc)^(1:5)

Which gives:

2568.116 2645.159 2724.514 2806.249 2890.437

But I would like to add them under columns in a nested tibble

Expected output:

Year 1      Year 2         ...          Year 5
2568.116    2645.159 2724.514 2806.249 2890.437

Data:

    myData = structure(list(purchasePrice = 419000, mtsSqrd = 125, mtsSquaredBins = structure(13L, .Label = c("(0,10]", 
"(10,20]", "(20,30]", "(30,40]", "(40,50]", "(50,60]", "(60,70]", 
"(70,80]", "(80,90]", "(90,100]", "(100,110]", "(110,120]", "(120,130]", 
"(130,140]", "(140,150]", "(150,160]", "(160,170]", "(170,180]", 
"(180,190]", "(190,200]", "(200,210]", "(210,220]", "(220,230]", 
"(230,240]", "(240,250]", "(250,260]", "(260,270]", "(270,280]", 
"(280,290]", "(290,300]", "(300,310]", "(310,320]", "(320,330]", 
"(330,340]", "(340,350]", "(350,360]", "(360,370]", "(370,380]", 
"(380,390]", "(390,400]", "(400,410]", "(410,420]", "(420,430]", 
"(430,440]", "(440,450]", "(450,460]", "(460,470]", "(470,480]", 
"(480,490]", "(490,500]", "(500,510]", "(510,520]", "(520,530]", 
"(530,540]", "(540,550]", "(550,560]", "(560,570]", "(570,580]", 
"(580,590]", "(590,600]"), class = "factor"), price_per_mtsSquared_rental_BINS = 19.9465286322085, 
    operatingExpensesPerc = 10, vacancy = 0.1, capRate = 0.08, 
    growthRateInc = 0.03, growthRateExp = 0.02, generalPotentialRev = 2493.31607902606, 
    generalVacancy = 249.331607902606, effGrossRev = 2243.98447112346, 
    operatingExpenses = 1250, netOpInc = 993.984471123455), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame"))

CodePudding user response:

We could store as a list with as.list or use as_tibble_row after creating a named vector

library(tibble)
library(dplyr)
library(stringr)
myData <- myData %>%
   mutate(
    generalPotentialRev_T = as_tibble_row(setNames(generalPotentialRev*(1   growthRateInc)^(1:T), str_c("Year", seq_len(T)))
  ))

-output

> myData$generalPotentialRev_T
# A tibble: 1 × 4
  Year1 Year2 Year3 Year4
  <dbl> <dbl> <dbl> <dbl>
1 2568. 2645. 2725. 2806.
  • Related