简体   繁体   中英

How to insert column in dataframe with different length from data frame in R

I want to insert a column measure_FL which has length 300 to a data frame which has 81 rows after Eligible.measures column. Structures of these two dataframes is given below

> str(FL)
'data.frame':   81 obs. of  31 variables:
 $ Sr.No                     : int  156 157 158 159 160 161 162 163 164 165 ...
 $ Uploaded.to.DB            : logi  NA NA NA NA NA NA ...
 $ Program.name              : chr  "Smart Saver Rebate Program Ceiling Insulation Upgrade" "Smart Saver Rebate Program Wall Insulation Upgrade" "Smart Saver Rebate Program High-Efficiency Chillers" "Smart Saver Rebate Program Unitary AC Units and Heat Pumps" ...
 $ Organization.name         : chr  "Duke Energy - Electric" "Duke Energy - Electric" "Duke Energy - Electric" "Duke Energy - Electric" ...
 $ State                     : chr  "FL" "FL" "FL" "FL" ...
 $ Type                      : chr  "Electric" "Electric" "Electric" "Electric" ...
 $ Project.type              : chr  "Energy Retrofit" "Energy Retrofit" "New Construction,Energy Retrofit" "New Construction,Energy Retrofit" ...
 $ Applicable.building.types : chr  "Multi-family,Hospital,Institution,Lodging,Office,Public assembly,Restaurant,School,Stand-alone retail,Strip mal"| __truncated__ "Hospital,Institution,Lodging,Office,Public assembly,Restaurant,School,Stand-alone retail,Strip mall,Supermarket"| __truncated__ "Hospital,Institution,Lodging,Office,Public assembly,Restaurant,School,Stand-alone retail,Strip mall,Supermarket"| __truncated__ "Hospital,Institution,Lodging,Office,Public assembly,Restaurant,School,Stand-alone retail,Strip mall,Supermarket"| __truncated__ ...
 $ Applicable.ECM.Category   : chr  "Building envelope" "Building envelope" "HVAC equipment" "HVAC equipment" ...
 $ Coverage                  : chr  "Equipment purchase,Labor/Installation cost" "Labor/Installation cost,Equipment purchase" "Equipment purchase,Labor/Installation cost" "Labor/Installation cost,Equipment purchase" ...
 $ Eligible.measures         : chr  "Ceiling insulation upgrade to R-38" "Wall insulation upgrade to R-20" "Air-cooled chillers; Water-cooled, Electrically operated positive displacement water chilling packages; Water-c"| __truncated__ "Air-Cooled Unitary AC and Heat Pumps (> 65,000 Btu/h); Water-Cooled Unitary AC and Heat Pumps; VFR Multi-Split "| __truncated__ ...
 $ Other.measures.considered.: chr  "No" "No" "No" "No" ...
 $ Borrower.category         : chr  "Owner,Tenant" "Owner,Tenant" "Owner,Tenant" "Owner,Tenant" ...
 $ Eligibility.criteria      : chr  "1. Commercial multifamily units will be qualified as individual units for incentive purposes.\n2. The weighted "| __truncated__ "1. Ensure facilities have both central electric AC and central electric heating (nonportable).\n2. If your wall"| __truncated__ "1. Chiller efficiencies and performance specifications should be calculated at Air-Conditioning, Heating, and R"| __truncated__ "1. Make sure all equipment meets Air-Conditioning, Heating, and Refrigeration Institute (AHRI) certification ef"| __truncated__ ...
 $ Documents.required        : chr  "1. Completed and signed online application form." "1. Completed and signed online application form." "1. Completed and signed online application form\n2. Certificate of Occupancy (new construction)" "1. Completed and signed online application form\n2. Certificate of Occupancy (new construction)" ...
 $ Disbursement.schedule     : chr  "No data" "No data" "No data" "No data" ...
 $ Program.URL               : chr  "https://www.duke-energy.com/business/products/smartsaver/building-envelope-improvements-fl" "https://www.duke-energy.com/business/products/smartsaver/building-envelope-improvements-fl" "https://www.duke-energy.com/business/products/smartsaver/hvac-equipment-fl" "https://www.duke-energy.com/business/products/smartsaver/hvac-equipment-fl" ...
 $ Contact.Number            : chr  "(877) 426-0009" "(877) 426-0009" "(877) 426-0009" "(877) 426-0009" ...
 $ Email.Address             : chr  "PrescriptiveIncentives@duke-energy.com" "PrescriptiveIncentives@duke-energy.com" "PrescriptiveIncentives@duke-energy.com" "PrescriptiveIncentives@duke-energy.com" ...
 $ Program.description       : chr  "A ceiling insulation check is a fast, simple way to lower monthly energy bills. Plus, you can earn significant "| __truncated__ "Wall insulation is a fast, simple way to lower monthly energy bills. Plus, you can earn significant incentives "| __truncated__ "Make your business more comfortable – and save money – when you install a high-efficiency chiller. Make cooling"| __truncated__ "A more efficient AC unit and heat pump can make your business environment more comfortable – and your employees"| __truncated__ ...
 $ Application.Process       : chr  "Step 1: Call Duke Energy for a free Business Energy Check.\nStep 2: Find a contractor and make improvements bas"| __truncated__ "Step 1: Call Duke Energy for a free Business Energy Check.\nStep 2: Find a contractor and make improvements bas"| __truncated__ "Step 1: Call Duke Energy for a free Business Energy Check.\nStep 2: Find a contractor and make improvements bas"| __truncated__ "Step 1: Call Duke Energy for a free Business Energy Check.\nStep 2: Find a contractor and make improvements bas"| __truncated__ ...
 $ Processing.time           : chr  "No data" "No data" "No data" "No data" ...
 $ Start.date                : chr  "December 1, 2020" "December 1, 2020" "December 1, 2020" "December 1, 2020" ...
 $ End.date                  : chr  "" "" "" "" ...
 $ Renewal.frequency         : chr  "No Data" "No Data" "No Data" "No Data" ...
 $ Security.Collateral       : chr  "None" "None" "None" "None" ...
 $ Maximum.amount            : chr  "" "" "$6,000" "$7,500" ...
 $ LTC                       : chr  "" "" "" "" ...
 $ Point.of.Contact          : chr  "" "" "" "" ...
 $ Created                   : chr  "April 30, 2022" "April 30, 2022" "April 30, 2022" "April 30, 2022" 
'data.frame':   300 obs. of  2 variables:
 $ measure_FL: Factor w/ 300 levels "Acuity – 75led",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Freq      : int  2 2 2 2 1 1 1 1 1 1 ...
> ```
Thank you for your help.

Combining data frames that contained different number of observations might be dangerous and somewhat unreasonable. Anyway, it is possible to do so.

df1 <- data.frame(a = c(1,2,3))
df2 <- data.frame(b = c(1,2,3,4,5))

cbind2 <- function(df1, df2){
  if(nrow(df1) < nrow(df2)){
    df1[(nrow(df1) + 1) : nrow(df2), ] <- NA
  }else{
    df2[(nrow(df2) + 1) : nrow(df1), ] <- NA
  }
  return(data.frame(cbind(df1, df2))) 
}

df3 <- cbind2(df1, df2)

Console output:

> df3
   a b
1  1 1
2  2 2
3  3 3
4 NA 4
5 NA 5

If we can append NA at the end, an option is

qpcR:::cbind.na(df1['measure_FL'], FL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM