将数据帧子集化为相等的子组块

时间:2014-02-19 08:59:02

标签: r dataframe subset

我有df个数据框,需要将子集划分为2 names的块。从下面的示例中,有4个唯一名称:a,b,c,d。我需要将子集分为2个列矩阵a,bc,d

输出格式:

name1
item_value
item_value
...
END
name2
item_value
item_value
...
END

示例:

#dummy data
df <- data.frame(name=sort(c(rep(letters[1:4],2),"a","a","c")),
                   item=round(runif(11,1,10)),
                   stringsAsFactors=FALSE)
#tried approach - split per name. I need to split per 2 names.
lapply(split(df,f=df$name),
       function(x) 
       {name <- unique(x$name)
        as.matrix(c(name,x[,2],"END"))
       })

#expected output
[,1] 
[1,] "a"  
[2,] "8"  
[3,] "9"  
[4,] "6"  
[5,] "4"  
[6,] "END"
[1,] "b"  
[2,] "2"  
[3,] "10" 
[4,] "END"

[,2] 
[1,] "c"  
[2,] "6"  
[3,] "6"  
[4,] "2"  
[5,] "END"
[1,] "d"  
[2,] "4"  
[3,] "1"  
[4,] "END"

注意:实际df包含~300000行,其中包含~35000个唯一名称。

2 个答案:

答案 0 :(得分:1)

不是从单个名称创建列表,而是从data.frame

的子集列中创建
res <- list("a_b" = c(df[df$name == "a",2],"END",df[df$name == "b", 2],"END"),
        "c_d" = c(df[df$name == "c",2],"END", df[df$name == "d", 2],"END"))

res2 <- vector(mode="list",length=2)
res2 <- sapply(1:(length(unique(df$name))/2),function(x) {
  sapply(seq(1,length(unique(df$name))-1,by=2), function(y) {
    name <- unique(df$name)
    res2[x] <- as.matrix(c(name[y],df[df$name == name[y],2],"END",name[y+1],df[df$name == name[y+1],2],"END"))
  })
})
answer <- res2[,1]

这给了我一个列表矩阵,因为有两个sapply正在发生,我想你想要的一切都在res2 [,1]

答案 1 :(得分:1)

你可以试试这个。

# for each 'name', "pad" 'item' with 'name' and 'END'
l1 <- lapply(split(df, f = df$name), function(x){
  name <- unique(x$name)
  as.matrix(c(name, x$item, "END")) 
  })

# create a sequence of numbers, to select two by two elements from the list
steps <- seq(from = 0, to = length(unique(df$name))/2, by = 2)

# loop over 'steps' to bind together list elements, two by two. 
l2 <- lapply(steps, function(x){
  do.call(rbind, l1[1:2 + x])
})

l2
# [[1]]
#      [,1] 
# [1,] "a"  
# [2,] "6"  
# [3,] "4"  
# [4,] "10" 
# [5,] "3"  
# [6,] "END"
# [7,] "b"  
# [8,] "6"  
# [9,] "7"  
# [10,] "END"
# 
# [[2]]
#     [,1] 
# [1,] "c"  
# [2,] "2"  
# [3,] "6"  
# [4,] "10" 
# [5,] "END"
# [6,] "d"  
# [7,] "5"  
# [8,] "4"  
# [9,] "END"