简体   繁体   English

R:如何从列中添加额外的行?

[英]R: How to make extra rows from a column?

I have a data-set of human hands, where currently a single person is defined as a single observation. 我有一个人手的数据集,目前单个人被定义为单个观察。 I want to reshape dataframe to have hands as individual observations. 我想重塑数据帧以将手作为单独的观察。 I tried something with "dplyr" package and "gather" function but had no success at all. 我尝试了“dplyr”包和“聚集”功能,但根本没有成功。

So from this, where each person is on one row : 所以,从这里,每个人都在一排:

id Gender Age   Present_R    Present_L    Dominant
1    F     2      TRUE         TRUE          R
2    F     5      TRUE         FALSE         L
3    M     8      FALSE        FALSE         R

to this, where each hand is on one row: 对此,每只手都在一排:

id Gender Age   Hand    Present  Dominant
1    F     2     R       TRUE     TRUE
2    F     2     L       TRUE     FALSE
3    F     5     R       TRUE     FALSE
4    F     5     L       FALSE    TRUE
5    M     8     R       FALSE    TRUE
6    M     8     L       FALSE    FALSE

Note that hand dominance becomes logical. 请注意,手优势变得合乎逻辑。

We can gather into 'long' format, arrange by 'id', then create the 'Dominant' by unlist ing the 'Present' columns, 'Hand' by removing the substring of the 'Hand' column 我们可以gather 'long'格式,按'id' arrange ,然后通过unlist 'Present'列来创建'Dominant','Hand'删除'Hand'列的子串

library(tidyverse)
gather(df1, Hand, Present, Present_R:Present_L) %>%
   arrange(id) %>%
   mutate(Dominant = unlist(df1[c("Present_L", "Present_R")]),
          id = row_number(),
          Hand = str_remove(Hand, ".*_"))
#   id Gender Age Dominant Hand Present
#1  1      F   2     TRUE    R    TRUE
#2  2      F   2    FALSE    L    TRUE
#3  3      F   5    FALSE    R    TRUE
#4  4      F   5     TRUE    L   FALSE
#5  5      M   8     TRUE    R   FALSE
#6  6      M   8    FALSE    L   FALSE

Based on the OP' comments, it seems like we need to compare the 'Dominant' with the 'Hand' 基于OP的评论,似乎我们需要将'Dominant'与'Hand'进行比较

gather(df1, Hand, Present, Present_R:Present_L) %>%
    arrange(id) %>% 
    mutate(id = row_number(),
           Hand = str_remove(Hand, ".*_"),
           Dominant = Dominant == Hand)
#   id Gender Age Dominant Hand Present
#1  1      F   2     TRUE    R    TRUE
#2  2      F   2    FALSE    L    TRUE
#3  3      F   5    FALSE    R    TRUE
#4  4      F   5     TRUE    L   FALSE
#5  5      M   8     TRUE    R   FALSE
#6  6      M   8    FALSE    L   FALSE

With a small data frame (ie, few variables, regardless of the number of cases), "hand-coding" may be the easiest approach: 使用小数据帧(即,少数变量,无论案例数量),“手动编码”可能是最简单的方法:

with(df, data.frame(id = c(id,id), Gender=c(Gender,Gender), Age=c(Age, Age), 
                    Hand = c(rep("R", nrow(df)), rep("L", nrow(df))),
                    Present = c(Present_R, Present_L),
                    Dominant = c(Dominant=="R", Dominant=="L")
                    ))

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

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