【问题标题】:TidyR get unique values across multiple columns and transform to column names?TidyR 跨多个列获取唯一值并转换为列名?
【发布时间】:2019-10-09 20:45:31
【问题描述】:

所以我试图通过将我的行设为学生姓名、列设为所有可用课程(HUDK4050 等)来整理数据集。但是,似乎我需要为每门独特的课程创建一个新专栏。这样做的最佳方法是什么?我已经通过更正间距和大小写问题等整理了一些代码。

  StudentName  Class1   Class2   Class3   Class4   Class5   Class6  
  <chr>        <chr>    <chr>    <chr>    <chr>    <chr>    <chr>   
1 Student1     ITSF4090 ITSF5008 ITSF5035 HUDK4050 NA       NA      
2 Student2     HUDK4050 QMSS5010 QMSS5015 QMSS5072 STAT4205 QMSS5021
3 Student3     HUDK4050 ITSF4090 ITSF4025 ITSF5035 NA       NA      
4 Student4     HUDK4050 HUDK4029 HUDK4052 CCPJ5062 A&HA4063 NA      
5 Student5     HUDK4050 HUDK4052 HUDK4029 NA       NA       NA      
6 Student6     HUDK4050 HUDM4125 HUDM5026 HUDM5126 NA       NA   

类似的东西

  StudentName  HUDK4050 HUDK4029   Class3   Class4   Class5   Class6  
  <chr>        <chr>    <chr>    <chr>    <chr>    <chr>    <chr>   
1 Student1     1         1
2 Student2     1         0
3 Student3     1         0
4 Student4     0         1
5 Student5     1         1
6 Student6     1         1

【问题讨论】:

  • 这些是众所周知的转换,称为 melt、cast、reshape。您想使用列 Class1,...,6 的值作为列名。有很多重复。
  • @smci 我实际上尝试查找重复项,因为我知道我已经看到了“将所有列转换为长,但首先,然后使用新的第二列转换为宽”的特定序列之前至少两次,但找不到一个。如果您知道这样的副本,我认为最好将其链接起来,这样我们就可以获得这些副本的规范副本。

标签: r unique tidyr


【解决方案1】:

我们可以转换为“长”格式,获取distinct元素,创建一列1,然后在tidyverse中重新整形为“宽”格式

library(dplyr)
library(tidyr) #1.0.0
df1 %>% 
   pivot_longer(cols = -StudentName, values_drop_na = TRUE) %>% 
   distinct(StudentName, value) %>% 
   mutate(n = 1) %>%
   pivot_wider(names_from = value, values_from= n, values_fill = list(n = 0))
# A tibble: 6 x 18
#  StudentName ITSF4090 ITSF5008 ITSF5035 HUDK4050 QMSS5010 QMSS5015 QMSS5072 STAT4205 QMSS5021 ITSF4025 HUDK4029 HUDK4052 CCPJ5062 `A&HA4063`
#  <chr>          <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>      <dbl>
#1 Student1           1        1        1        1        0        0        0        0        0        0        0        0        0          0
#2 Student2           0        0        0        1        1        1        1        1        1        0        0        0        0          0
#3 Student3           1        0        1        1        0        0        0        0        0        1        0        0        0          0
#4 Student4           0        0        0        1        0        0        0        0        0        0        1        1        1          1
#5 Student5           0        0        0        1        0        0        0        0        0        0        1        1        0          0
#6 Student6           0        0        0        1        0        0        0        0        0        0        0        0        0          0
# … with 3 more variables: HUDM4125 <dbl>, HUDM5026 <dbl>, HUDM5126 <dbl>

数据

df1 <- structure(list(StudentName = c("Student1", "Student2", "Student3", 
"Student4", "Student5", "Student6"), Class1 = c("ITSF4090", "HUDK4050", 
"HUDK4050", "HUDK4050", "HUDK4050", "HUDK4050"), Class2 = c("ITSF5008", 
"QMSS5010", "ITSF4090", "HUDK4029", "HUDK4052", "HUDM4125"), 
    Class3 = c("ITSF5035", "QMSS5015", "ITSF4025", "HUDK4052", 
    "HUDK4029", "HUDM5026"), Class4 = c("HUDK4050", "QMSS5072", 
    "ITSF5035", "CCPJ5062", NA, "HUDM5126"), Class5 = c(NA, "STAT4205", 
    NA, "A&HA4063", NA, NA), Class6 = c(NA, "QMSS5021", NA, NA, 
    NA, NA)), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6"))

【讨论】:

  • 感谢 akrun,你从哪里得到这个“价值”? distinct(StudentName, value),只是标准吗?
  • @adwang31 默认情况下,如果在pivot_longer中不指定要命名的列,它将命名为'value'和'name'
【解决方案2】:

这也是一种先转换为长的方法,我使用melt,然后在转换为宽时将所有前一列内容的新列用作输出列,我使用@987654322 @。

libary(data.table)

dcast(melt(df, 1, na.rm = T), StudentName ~ value, value.var = 'variable', 
      fun.aggregate = length)


#   StudentName A&HA4063 CCPJ5062 HUDK4029 HUDK4050 HUDK4052 HUDM4125 HUDM5026 HUDM5126 ITSF4025
# 1    Student1        0        0        0        1        0        0        0        0        0
# 2    Student2        0        0        0        1        0        0        0        0        0
# 3    Student3        0        0        0        1        0        0        0        0        1
# 4    Student4        1        1        1        1        1        0        0        0        0
# 5    Student5        0        0        1        1        1        0        0        0        0
# 6    Student6        0        0        0        1        0        1        1        1        0
#   ITSF4090 ITSF5008 ITSF5035 QMSS5010 QMSS5015 QMSS5021 QMSS5072 STAT4205
# 1        1        1        1        0        0        0        0        0
# 2        0        0        0        1        1        1        1        1
# 3        1        0        1        0        0        0        0        0
# 4        0        0        0        0        0        0        0        0
# 5        0        0        0        0        0        0        0        0
# 6        0        0        0        0        0        0        0        0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2019-08-26
    • 2017-09-21
    相关资源
    最近更新 更多