简体   繁体   English

在r中配对列并使数据框从宽到长

[英]Pairing columns and making dataframe from wide to long in r

I have this kind of dataframe :我有这种dataframe

id institution  name_a      info_a bfullname      idb
1  A            Chet Baker  666    Clifford Brown 123

I need to reshape it, keeping the id , institution and pair the columns keeping the values like this:我需要重塑它,保持idinstitution和配对列保持这样的值:

id institution role       name           id_name
1  A           student    Chet Baker     666
1  A           teacher    Clifford Brown 123

The role column is defined by the column name , which I have an id vector identifying like this:角色列由column name定义,我有一个 id 向量标识如下:

value     id 
name_a    student
bfullname teacher

The problem is that I have a lot of columns with different names, I needed an way to specify which ones go along with another, or maybe a solution that I could rename the columns and do so.问题是我有很多具有不同名称的列,我需要一种方法来指定哪些列与另一个一起使用,或者可能是一种可以重命名列并这样做的解决方案。

I've seen a lot of reshape , dcast , melt and so on topics but still couldn't figure it out我看过很多reshapedcastmelt等等话题,但还是想不通

Any ideas how to do it?任何想法如何做到这一点?

Forget reshape , use tidyr :忘记reshape ,使用tidyr

require(dplyr)
require(tidyr)

df <- tribble(
~id, ~institution,  ~name_a,      ~info_a, ~bfullname,      ~idb,
1,  "A",            "Chet Baker",  666,    "Clifford Brown", 123,
2,  "B",            "George Baker",  123,    "Charlie Brown", 234,
3,  "C",            "Banket Baker",  456,    "James Brown", 647,
4,  "D",            "Koeken Baker",  789,    "Golden Brown", 967
)

def <- tribble(~value, ~roleid, ~info,
"name_a",    "student", "info_a", 
"bfullname", "teacher", "idb")


def    

dflong <- df %>%
  gather(key, value, -id, -institution)


dflong %>%
  filter(key %in% def$value) %>%
  rename(role = key, name = value) %>%
  inner_join(def, by = c('role' = 'value')) %>%
  left_join(dflong %>% select(- institution), by = c('id' = 'id','info' = 'key'))

Which will result in:这将导致:

# A tibble: 8 x 7
id institution role      name           roleid  info   value
      <dbl> <chr>       <chr>     <chr>          <chr>   <chr>  <chr>
1     1 A           name_a    Chet Baker     student info_a 666  
2     2 B           name_a    George Baker   student info_a 123  
3     3 C           name_a    Banket Baker   student info_a 456  
4     4 D           name_a    Koeken Baker   student info_a 789  
5     1 A           bfullname Clifford Brown teacher idb    123  
6     2 B           bfullname Charlie Brown  teacher idb    234  
7     3 C           bfullname James Brown    teacher idb    647  
8     4 D           bfullname Golden Brown   teacher idb    967  
library(data.table)

setDT(df)

melt(
  df,
  id.vars = 1:2, 
  measure.vars = list(name = c(3, 5), id_name = c(4, 6)),
  variable.name = "role"
)
#>    id institution role           name id_name
#> 1:  1           A    1     Chet Baker     666
#> 2:  1           A    2 Clifford Brown     123

Where df is: df在哪里:

df <- read.table(text = '
id institution  name_a      info_a bfullname      idb
1  A            "Chet Baker"  666    "Clifford Brown" 123
', header = TRUE)

Created on 2019-02-14 by the reprex package (v0.2.1)reprex 包(v0.2.1) 于 2019 年 2 月 14 日创建

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

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