简体   繁体   English

创建一个 dataframe 的列,该列将包含一个列的唯一值和另一个 dataframe 的唯一列名

[英]Create a dataframe with a column that will include unique values of a column and unique column names of another dataframe

I have the dataframe below:我在下面有 dataframe:

dummy<-structure(list(Name = c("A", "B", "C", "A", "B", "C"), `#BISB` = c(2, 
6, 4, 0, 4, 6), `#BISC` = c(2, 6, 4, 0, 4, 6), `#BISD` = c(2, 
6, 4, 0, 4, 6), `#BISE` = c(2, 6, 4, 0, 4, 6), `#BISF` = c(2, 
6, 4, 0, 4, 6), `#BISG` = c(2, 6, 4, 0, 4, 6), `#BISH` = c(2, 
6, 4, 0, 4, 6)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

and I would like to reshape it in a nodes dataframe.我想在nodes dataframe 中重塑它。 This nodes dataframe should contain one column named id with index numbers ( 1,2,3 ...) and the second column will be named group and it will contain all the unique values of the column Name and all the unique column names except from the column Name from the dummy dataframe like:nodes dataframe 应包含一列名为id且索引号为 ( 1,2,3 ...) 的列,第二列将命名为group ,它将包含列Name的所有唯一值和所有唯一列名,除了 from来自dummy dataframe 的列Name如下:

id group
1   1     A
2   2     B
3   3     C
4   4 #BISB
5   5 #BISC
6   6 #BISD
7   7 #BISE
8   8 #BISF
9   9 #BISG
10 10 #BISH

We may create a tibble with summarise by taking the unique values of 'Name', concatenate ( c ) with the column names except the first and then create the 'id' as the sequence ( row_number() )我们可以通过将“名称”的unique值与除第一个以外的列名连接( c )的唯一值来创建一个带有summarise的小标题,然后将“id”创建为序列( row_number()

library(dplyr)
dummy %>% 
  summarise(group = c(unique(Name), names(.)[-1])) %>%
  mutate(id = row_number(), .before = 1)

-output -输出

# A tibble: 10 × 2
      id group
   <int> <chr>
 1     1 A    
 2     2 B    
 3     3 C    
 4     4 #BISB
 5     5 #BISC
 6     6 #BISD
 7     7 #BISE
 8     8 #BISF
 9     9 #BISG
10    10 #BISH

Or more easily或者更容易

out <- data.frame(group = c(unique(dummy$Name), names(dummy)[-1]))
out$id <- seq_len(nrow(out))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM