R将数据帧的每一行拆分为两行

时间:2016-08-27 10:12:10

标签: r

我想将数据帧(numberic)的每一行拼接成两行。例如,像这样的原始数据框的一部分(nrow(原始数据框)> 2800000):

ID X Y Z value_1 value_2
1  3 2 6     22       54
6 11 5 9     52       71
3  7 2 5      2       34
5 10 7 1     23       47

在分割每一行之后,我们可以得到:

ID  X  Y  Z  
1   3  2  6  
22 54 NA NA  
6  11  5  9  
52 71 NA NA  
3   7  2  5  
2  34 NA NA  
5  10  7  1  
23 47 NA NA  

将“value_1”和“value_2”列拆分,并将每个元素设置为新行。例如,value_1 = 22和value_2 = 54设置为新行。

3 个答案:

答案 0 :(得分:1)

以下是data.table的一个选项。我们转换了&#39; data.frame&#39;到&#39; data.table&#39;通过创建一个rownames列(setDT(df1, keep.rownames = TRUE))。使用1:5选项1, 6, 7list rbind元素对listfill = TRUE列进行子集,以便为相应的列返回NA在其中一个数据集中找到order行号(&#39; rn&#39;)并将行号列分配(:=)为&#39; NULL&#39;。< / p>

library(data.table)
setDT(df1, keep.rownames = TRUE)[]
rbindlist(list(df1[, 1:5, with = FALSE], setnames(df1[, c(1, 6:7),
   with = FALSE], 2:3, c("ID", "X"))), fill = TRUE)[order(rn)][, rn:= NULL][]
#    ID  X  Y  Z
#1:  1  3  2  6
#2: 22 54 NA NA
#3:  6 11  5  9
#4: 52 71 NA NA
#5:  3  7  2  5
#6:  2 34 NA NA
#7:  5 10  7  1
#8: 23 47 NA NA

对应于上述逻辑的hadleyverse将是

library(dplyr)
tibble::rownames_to_column(df1[1:4]) %>% 
         bind_rows(., setNames(tibble::rownames_to_column(df1[5:6]), 
                         c("rowname", "ID", "X"))) %>% 
         arrange(rowname) %>% 
         select(-rowname)
#   ID  X  Y  Z
#1  1  3  2  6
#2 22 54 NA NA
#3  6 11  5  9
#4 52 71 NA NA
#5  3  7  2  5
#6  2 34 NA NA
#7  5 10  7  1
#8 23 47 NA NA

数据

df1 <- structure(list(ID = c(1L, 6L, 3L, 5L), X = c(3L, 11L, 7L, 10L
), Y = c(2L, 5L, 2L, 7L), Z = c(6L, 9L, 5L, 1L), value_1 = c(22L, 
52L, 2L, 23L), value_2 = c(54L, 71L, 34L, 47L)), .Names = c("ID", 
"X", "Y", "Z", "value_1", "value_2"), class = "data.frame",
 row.names = c(NA, -4L))

答案 1 :(得分:0)

这是一个(非常慢)纯R解决方案,不使用额外的包:

/* jQuery scroll to element on click */

$(document).ready(function(){

    $('nav#site-navigation ul li').click(function(){
    $('html, body').animate({
        scrollTop: $( $(this).attr('href') ).offset().top
    }, 1000);
    return false;
    });

});

结果是

# Replicate your matrix
input_df <- data.frame(ID = rnorm(10000),
                           X = rnorm(10000),
                           Y = rnorm(10000),
                           Z = rnorm(10000),
                           value_1 = rnorm(10000),
                           value_2 = rnorm(10000))

# Preallocate memory to a data frame
output_df <- data.frame(
    matrix(
      nrow = nrow(input_df)*2,
      ncol = ncol(input_df)-2))

# Loop through each row in turn.
# Put the first four elements into the current 
# row, and the next two into the current+1 row
# with two NAs attached.
for(i in seq(1, nrow(output_df), 2)){
  output_df[i,] <- input_df[i, c(1:4)]
  output_df[i+1,] <- c(input_df[i, c(5:6)],NA,NA)
}

colnames(output_df) <- c("ID", "X", "Y", "Z")

答案 2 :(得分:0)

这应该有效

data <- read.table(text= "ID X Y Z value_1 value_2
           1  3 2 6     22       54
           6 11 5 9     52       71
           3  7 2 5      2       34
           5 10 7 1     23       47", header=T)

data1 <- data[,1:4]
data2 <- setdiff(data,data1)
names(data2) <- names(data1)[1:ncol(data2)]

combined <- plyr::rbind.fill(data1,data2)
n <- nrow(data1)
combined[kronecker(1:n, c(0, n), "+"),]

虽然你为什么需要这样做,但这还是打败了我。