如何使彼此相邻排?

时间:2019-09-09 15:28:04

标签: r dataframe

我有一个不适用的列或行的索引。我想建立一个新的数据集,并将这些行彼此相邻。

示例

            data      rowindex
            1         NA
            2         NA
            3         1,2
            4         5
            5         NA

在第3行中,rowindex列是1和2,所以我将第一行和第三行彼此相邻,并将第二行和第三行彼此相邻。另外,rowindex的第四列是5,所以我将第五行与第五行相邻。

输出

          data      rowindex      data.1      rowindex.1
            1         NA             3              1
            2         NA             3              1
            5         NA             4              5

我忽略了写下其他专栏。如果我们有2个索引,则输出中有2个不同的行。

2 个答案:

答案 0 :(得分:0)

这似乎很奇怪,但是您可以进行以下操作:

ind = !is.na(dd$rowindex)
cbind(dd[dd$rowindex[ind], ],
      dd[ind, ])
#   data rowindex data rowindex
# 1    1       NA    3        1
# 5    5       NA    4        5

使用此数据:

dd = read.table(text = 'data      rowindex
            1         NA
            2         NA
            3         1
            4         5
            5         NA', header= T)

答案 1 :(得分:0)

欢迎社区!尝试以下操作:


df <- read.table(text="            data      rowindex
            1         NA
                 2         NA
                 3         1,2
                 4         5
                 5         NA",header=T)

## call in a library to manipulate text:
library(dplyr)
library(stringr)
library(tidyr)

## And now use left_join() using the second column as the key:

## first throw away NAs, and split delimiters

df1 <-
  df %>% filter(!is.na(rowindex)) %>% mutate(ri = str_split(string = rowindex, pattern = ",")) %>% 
    unnest %>% mutate(ri = as.integer(ri))
df1

## now you can combine this one with the first one:
left_join(df1, df, by = c("ri" = "data"), )

您可以在此处找到有关left_join的更多信息:https://dplyr.tidyverse.org/articles/programming.html

相关问题