简体   繁体   English

如何将某些列转换为R中的行

[英]how to convert certain columns as rows in r

I have a data frame like this 我有一个这样的数据框

Htno         Subname             marks      credits
15mq1a0501     abc                43          3
15mq1a0501     xyz                55          6
15mq1a0502     abc                56          3
15mq1a0502     xyz                60          6
15mq1a0503     abc                10          0
15mq1a0503     xyz                56          6

now i need a data frame to be converted like this 现在我需要一个像这样转换的数据框

Htno               abc         xyz       Totalmarks        Totalcredits
15mq1a0501          43         55         98                  9
15mq1a0502          56         60        116                  9
15mq1a0503          10         56         66                  6

I used dplyr package but I am not able to do so. 我使用了dplyr软件包,但无法这样做。

You could use the following: 您可以使用以下内容:

require(tidyverse)
df %>%
  spread(Subname, marks) %>%
  group_by(HTno) %>%
  summarise(abc = max(abc, na.rm = T), xyz = max(xyz, na.rm = T), Totalcredits = sum(credits)) %>%
  mutate(Totalmarks = abc + xyz)

The result would be: 结果将是:

        HTno   abc   xyz Totalcredits Totalmarks
      <fctr> <dbl> <dbl>        <dbl>      <dbl>
1 15mq1a0501    43    55            9         98
2 15mq1a0502    56    60            9        116
3 15mq1a0503    10    56            6         66

Just an alternative that only uses dplyr functions. 只是仅使用dplyr函数的替代方法。 Note that the solution can be tedious when Subname has many factors. 请注意,当Subname有很多因素时,解决方案可能很乏味。 See if others can have a more general solution. 看看其他人是否可以有一个更通用的解决方案。

library(magrittr)
library(dplyr)

df %>% group_by(Htno) %>% 
  summarize(abc = marks[Subname == "abc"],
            xyz = marks[Subname == "xyz"],
            Totalmarks = sum(marks), 
            Totalcredits = sum(credits))

Edit: The below generalisation works, but it is more complicated and needs tidyr::spread . 编辑:下面的归纳工作,但更复杂,需要tidyr::spread

library(magrittr)
library(dplyr)

library(tidyr)    
df_1 <- df %>% group_by(Htno) %>% 
  summarize(Totalmarks = sum(marks), 
            Totalcredits = sum(credits))

df_2 <- df %>% select(-credits) %>% spread(Subname, marks) %>% 
  group_by(Htno) %>% summarize_each(funs(mean))

left_join(df_2, df_1, by = "Htno", all = TRUE)

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

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