将起始行作为变量附加到数据框

时间:2017-02-24 10:34:14

标签: r dataframe data-manipulation

我有一个csv的数据如下,其中上部是一些元数据,下面是数据框。

> dput(sampledata2)
structure(list(X1 = c("Name", "ID1", "ID2", "Date", NA, "Var1", 
"1", "2", "3"), X2 = c("some name", "1", "2", "24-02-2017", NA, 
"Var2", "2", "3", "4"), X3 = c(NA, NA, NA, NA, NA, "Var3", "3", 
"4", "5"), X4 = c(NA, NA, NA, NA, NA, "Var4", "4", "5", "6"), 
    X5 = c(NA, NA, NA, NA, NA, "Var5", "5", "6", "7")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -9L), .Names = c("X1", 
"X2", "X3", "X4", "X5"), spec = structure(list(cols = structure(list(
    X1 = structure(list(), class = c("collector_character", "collector"
    )), X2 = structure(list(), class = c("collector_character", 
    "collector")), X3 = structure(list(), class = c("collector_character", 
    "collector")), X4 = structure(list(), class = c("collector_character", 
    "collector")), X5 = structure(list(), class = c("collector_character", 
    "collector"))), .Names = c("X1", "X2", "X3", "X4", "X5")), 
    default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

现在,我希望将元数据作为数据的变量包含在下面

> sampledata
# A tibble: 3 × 9
   Var1  Var2  Var3  Var4  Var5      Name   ID1   ID2       Date
  <int> <int> <int> <int> <int>     <chr> <int> <int>      <chr>
1     1     2     3     4     5 some name     1     2 24-02-2017
2     2     3     4     5     6 some name     1     2 24-02-2017
3     3     4     5     6     7 some name     1     2 24-02-2017

1 个答案:

答案 0 :(得分:4)

正如@Sotos所说:奇怪的数据结构!但是,您可以按如下方式修复它:

sampledata <- as.data.frame(sampledata)  # needed to prevent unwanted behavior from 'tbl_df'-class

d1 <- sampledata[7:9,]
names(d1) <- unlist(sampledata[6,])
d1

d2 <- as.data.frame(t(sampledata[1:4,2]))
names(d2) <- sampledata[1:4,1]
d2

res <- cbind(d1,d2)

给出:

> res
  Var1 Var2 Var3 Var4 Var5      Name ID1 ID2       Date
1    1    2    3    4    5 some name   1   2 24-02-2017
2    2    3    4    5    6 some name   1   2 24-02-2017
3    3    4    5    6    7 some name   1   2 24-02-2017