使用R中的索引重命名多个列

时间:2018-01-10 19:25:49

标签: r dataframe dplyr

我从json文件中删除了这些数据,该文件提供了交互式图形。

df <- structure(list(x = c("Iraq", "Syria"), Aug. = c("120", "12"), 
    Sept. = c("118", "78"), Oct. = c("184", "251"), Nov. = c("197", 
    "160"), Dec. = c("263", "169"), Jan. = c("303", "322"), Feb. = c("268", 
    "195"), March = c("304", "126"), Apr. = c("391", "145"), 
    May = c("472", "205"), June = c("407", "215"), July = c("518", 
    "371"), Aug. = c("522", "210"), Sept. = c("525", "127"), 
    Oct. = c("502", "117"), Nov. = c("530", "232"), Dec. = c("514", 
    "216"), Jan. = c("554", "170"), Feb = c("442", "218"), `March ` = c("449", 
    "132"), April = c("548", "150"), May = c("504", "178"), June = c("483", 
    "375"), July = c("332", "352"), Aug = c("259", "303"), Sept = c("291", 
    "335"), `Oct ` = c("301", "273"), Nov = c("280", "351"), 
    Dec = c("185", "318"), `Jan ` = c("234", "535"), `Feb ` = c("272", 
    "547"), Mar = c("268", "434"), `April ` = c("292", "548"), 
    May = c("267", "611"), June = c("229", "878"), `July ` = c("224", 
    "966"), `Aug ` = c("292", "1463"), `Sept ` = c("253", "1278"
    ), `Oct ` = c("196", "499"), `Nov ` = c("85", "139"), Dec = c("28", 
    "184"), `Jan 4th` = c("0", "26")), .Names = c("x", "Aug.", 
"Sept.", "Oct.", "Nov.", "Dec.", "Jan.", "Feb.", "March", "Apr.", 
"May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec.", 
"Jan.", "Feb", "March ", "April", "May", "June", "July", "Aug", 
"Sept", "Oct ", "Nov", "Dec", "Jan ", "Feb ", "Mar", "April ", 
"May", "June", "July ", "Aug ", "Sept ", "Oct ", "Nov ", "Dec", 
"Jan 4th"), row.names = c(NA, -2L), class = c("tbl_df", "tbl", 
"data.frame"))

此数据框中的数据时间超过三年,但只有月数以列名的形式给出。我想要做的是首先通过粘贴每月背后的正确年份来重命名colums。然后使用gather()进行转置(现在不能正常工作,因为多列具有相同的名称)。

我可以想象这样的事情:

df %>%
  rename(c(2:6) = paste("2014"),
         c(7:18) = paste("2015"),
         c(19:30) = paste("2016"))

我并不感到惊讶,但是没有用,但也许你得到了要点

3 个答案:

答案 0 :(得分:1)

要更改数据框的列名,您可以执行以下操作:

beforeEach(() => {
    fixture = TestBed.createComponent(HomeComponent);
    component = fixture.componentInstance;
    debugEl = fixture.debugElement;
    element = debugEl.nativeElement;
    fixture.detectChanges();
  });
  it('should have href with /login', () => {
    fixture.detectChanges();
    const href = debugEl.query(By.css('a')).nativeElement.getAttribute('href');
    console.error(href) // give me null 
    //expect(expect(href)).toEqual('/login');
  });

答案 1 :(得分:1)

如果您使用更多标准化列名称(例如,2月与2月),它也可能会让您后悔。我在这里用lubridate来排序日期。

require(lubridate)
colnames(df)[-1] <- paste(months(seq(ymd('2014-08-01'),ymd('2018-01-01'), by = 'months'), abbreviate = T), 
                          year(seq(ymd('2014-08-01'),ymd('2018-01-01'), by = 'months')))

答案 2 :(得分:0)

这些步骤可以帮助您:

colname <- names(df)

colname[2:6] <- paste(colname[2:6], "2014")
colname[7:18] <- paste(colname[7:18], "2015")
colname[19:30] <- paste(colname[19:30], "2016")

#Now set the column name in data frame
colnames(df) <- colname


# Column names after modification
#>names(df)
# [1] "x"           "Aug. 2014"   "Sept. 2014"  "Oct. 2014"   "Nov. 2014"   #"Dec. 2014"   "Jan. 2015"   "Feb. 2015"   "March 2015"  "Apr. 2015"  
#[11] "May 2015"    "June 2015"   "July 2015"   "Aug. 2015"   "Sept. 2015"  #"Oct. 2015"   "Nov. 2015"   "Dec. 2015"   "Jan. 2016"   "Feb 2016"   
#[21] "March  2016" "April 2016"  "May 2016"    "June 2016"   "July 2016"   "Aug #2016"    "Sept 2016"   "Oct  2016"   "Nov 2016"    "Dec 2016"   
#[31] "Jan "        "Feb "        "Mar"         "April "      "May"         #"June"        "July "       "Aug "        "Sept "       "Oct "       
#[41] "Nov "        "Dec"         "Jan 4th"