R:根据其他行值重命名列表中的列

时间:2015-07-21 10:06:58

标签: r list rename names

我从matlab导入数据并有一个大的列表(超过1000个列表元素),我从中创建了以下样本数据集data,只有两个列表元素。

data <- structure(list(TEST.DATA.1.1 = structure(list(ID = c(2, 2, 2), YEAR = c(1990, 1991, 1992), DATA.1 = c(10, 20, 30), DATA.NAME = structure(c(1L, 1L, 1L), class = "factor", .Label = "Test"), Remarks = c(1990, 1991, 1992)), .Names = c("ID", "YEAR", "DATA.1", "DATA.NAME", "Remarks"), row.names = c(NA, -3L), class = "data.frame"), TEST.DATA.2.1 = structure(list(ID = c(4, 4), YEAR = c(2000, 2001), DATA.1 = c(55, 60), DATA.2 = c(0, 2), DATA.3 = c(4, 6), DATA.NAME.structure..n1....Dim...c.1L..1L.. = structure(c(1L,1L), class = "factor", .Label = "n1"), DATA.NAME.structure..n2....Dim...c.1L..1L.. = structure(c(1L, 1L), class = "factor", .Label = "n2"), DATA.NAME.structure..n3....Dim...c.1L..1L.. = structure(c(1L,1L), class = "factor", .Label = "n3"), Remarks = c(2000,2001)), .Names = c("ID", "YEAR", "DATA.1", "DATA.2", "DATA.3", "DATA.NAME.structure..n1....Dim...c.1L..1L..", "DATA.NAME.structure..n2....Dim...c.1L..1L..", "DATA.NAME.structure..n3....Dim...c.1L..1L..", "Remarks"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("TEST.DATA.1.1", "TEST.DATA.2.1"))

data
$TEST.DATA.1.1
  ID YEAR DATA.1 DATA.NAME Remarks
1  2 1990     10      Test    1990
2  2 1991     20      Test    1991
3  2 1992     30      Test    1992

$TEST.DATA.2.1
  ID YEAR DATA.1 DATA.2 DATA.3 DATA.NAME.structure..n1....Dim...c.1L..1L.. DATA.NAME.structure..n2....Dim...c.1L..1L.. DATA.NAME.structure..n3....Dim...c.1L..1L.. Remarks
1  4 2000     55      0      4                                          n1                                          n2                                          n3    2000
2  4 2001     60      2      6                                          n1                                          n2                                          n3    2001

我正在寻找一种方法,我可以使用列DATA.NAME中的名称重命名数据列。有时存在多个数据列和相应的名称,例如在第二个列表元素中,有时只有一个例如在第一个元素中。我正在寻找一种方法来重命名一个大型列表(&gt; 1000个列表元素),然后删除DATA.NAME列,例如data_new

 data_new
  $TEST.DATA.1.1
      ID YEAR Test Remarks
    1  2 1990   10    1990
    2  2 1991   20    1991
    3  2 1992   30    1992

    $TEST.DATA.2.1
      ID YEAR n1 n2 n3 Remarks
    1  4 2000 55  0  4    2000
    2  4 2001 60  2  6    2001

3 个答案:

答案 0 :(得分:3)

使用data.table包的解决方案。

require(data.table)

data <- structure(list(TEST.DATA.1.1 = structure(list(ID = c(2, 2, 2), YEAR = c(1990, 1991, 1992), DATA.1 = c(10, 20, 30), DATA.NAME = structure(c(1L, 1L, 1L), class = "factor", .Label = "Test"), Remarks = c(1990, 1991, 1992)), .Names = c("ID", "YEAR", "DATA.1", "DATA.NAME", "Remarks"), row.names = c(NA, -3L), class = "data.frame"), TEST.DATA.2.1 = structure(list(ID = c(4, 4), YEAR = c(2000, 2001), DATA.1 = c(55, 60), DATA.2 = c(0, 2), DATA.3 = c(4, 6), DATA.NAME.structure..n1....Dim...c.1L..1L.. = structure(c(1L,1L), class = "factor", .Label = "n1"), DATA.NAME.structure..n2....Dim...c.1L..1L.. = structure(c(1L, 1L), class = "factor", .Label = "n2"), DATA.NAME.structure..n3....Dim...c.1L..1L.. = structure(c(1L,1L), class = "factor", .Label = "n3"), Remarks = c(2000,2001)), .Names = c("ID", "YEAR", "DATA.1", "DATA.2", "DATA.3", "DATA.NAME.structure..n1....Dim...c.1L..1L..", "DATA.NAME.structure..n2....Dim...c.1L..1L..", "DATA.NAME.structure..n3....Dim...c.1L..1L..", "Remarks"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("TEST.DATA.1.1", "TEST.DATA.2.1"))

fun <- function(x) {
  x <- data.table(x)
  var1 <- grep("DATA.[0-9]", names(x), value = T)
  var2 <- as.character(unlist(x[1, grep("DATA.NAME", names(x)), with = F]))
  setnames(x, var1, var2)
  x[, grep("DATA.NAME", names(x)) := NULL, with = F]
  return(x)
}

data_new <- lapply(data, fun)

答案 1 :(得分:3)

这是一个基础R方法:

for (i in seq_along(data)) {
    namecis <- grep('^DATA\\.NAME',names(data[[i]]));
    datacis <- grep('^DATA\\.\\d+',names(data[[i]]));
    names(data[[i]])[datacis] <- as.character(unlist(data[[i]][1,namecis]));
    data[[i]][namecis] <- list(NULL);
};
data;
## $TEST.DATA.1.1
##   ID YEAR Test Remarks
## 1  2 1990   10    1990
## 2  2 1991   20    1991
## 3  2 1992   30    1992
##
## $TEST.DATA.2.1
##   ID YEAR n1 n2 n3 Remarks
## 1  4 2000 55  0  4    2000
## 2  4 2001 60  2  6    2001

答案 2 :(得分:2)

这应该有用......

library(dplyr)

for (i in 1:length(data))
{

  d <- data[[i]]

  # Find the new names
  new_names <- select(d, starts_with('DATA.NAME'))
  new_names <- unlist(new_names[1,])
  names(new_names) <- NULL
  new_names <- as.character(new_names)

  # Remove the columns containing the names
  d <- select(d, -starts_with('DATA.NAME'))

  # Pick which columns we want to replace
  old_names <- names(d)
  to_replace <- grep('DATA.[0-9]+', old_names)

  # Replace those names
  names(d)[to_replace] <- new_names

  #Replace the list element 
  data[[i]] <- d

}