微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据变量名称的首字母将数据整形为长格式

如何解决根据变量名称的首字母将数据整形为长格式

我正在尝试根据变量名称的第一个字母将我的数据重塑为长格式。我有来自母亲和父亲的数据,它们由变量的第一个字母表示,就像在这个数据集中:

toydat <- data.frame(id=1:10,mincome=rep(sample(1:5),2),medu=rep(sample(1:5),methnicity=rep(sample(1:5),fincome=rep(sample(1:5),fedu=rep(sample(1:5),fethnicity=rep(sample(1:5),2)
)

最终数据应该是这样的

 gender income   edu ethnicity 
 mother      3     4         3
 mother      2     2         4
 mother      5     3         2
 mother      3     4         2
 mother      4     3         3
 mother      2     2         1
 mother      3     3         4
 mother      4     4         4
 mother      3     3         5
 mother      2     2         1
 father      5     5         2
 father      3     3         3
 father      4     2         2
 father      2     2         4
 father      3     1         5
 father      4     4         1
 father      4     5         2
 father      3     2         3
 father      3     3         2
 father      1     2         1

任何帮助将不胜感激!

编辑 感谢@akrun,我原来的问题解决了。我想知道如果性别指示符 mf 位于名称的末尾会怎样。如何以正则表达式的方式names_sep

通过尝试以下代码,尽管创建了性别变量,但变量并未拆分。

toydat %>% 
     select(-id) %>% 
     pivot_longer(cols = everything(),names_to = c(".value","gender"),names_sep = "(<=[a-z])(?=[mf]$)") %>%
     mutate(gender = case_when(gender == 'm' ~ 'mother',TRUE ~ 'father'))
# A tibble: 10 x 7
   gender mincome  medu methnicity fincome  fedu fethnicity
   <chr>    <int> <int>      <int>   <int> <int>      <int>
 1 father       1     3          4       5     5          5
 2 father       5     4          3       3     1          4
 3 father       3     2          2       1     4          2
 4 father       2     1          1       4     2          1
 5 father       4     5          5       2     3          3
 6 father       1     3          4       5     5          5
 7 father       5     4          3       3     1          4
 8 father       3     2          2       1     4          2
 9 father       2     1          1       4     2          1
10 father       4     5          5       2     3          3

解决方法

我们删除 'id' 列,然后将所有列转为长格式,指定 names_sep 在开头的 'm' 或 'f' 之间拆分 (^)在正则表达式环顾中将字符串和下一个字母重新编码,然后通过将 case_when

中的 'm' 更改为 'mother' 并将 'f' 更改为 'father' 来重新编码 'gender' 列
library(dplyr)
library(tidyr)
toydat %>% 
   select(-id) %>% 
   pivot_longer(cols = everything(),names_to = c("gender",".value"),names_sep = "(?<=^[mf])(?=[a-z])") %>%
   mutate(gender = case_when(gender == 'm' ~ 'mother',TRUE ~ 'father'))

-输出

# A tibble: 20 x 4
#   gender income   edu ethnicity
#   <chr>   <int> <int>     <int>
# 1 mother      3     5         3
# 2 father      4     5         5
# 3 mother      4     3         5
# 4 father      3     1         1
# 5 mother      2     1         2
# 6 father      2     3         3
# 7 mother      1     2         1
# 8 father      5     2         4
# 9 mother      5     4         4
#10 father      1     4         2
#11 mother      3     5         3
#12 father      4     5         5
#13 mother      4     3         5
#14 father      3     1         1
#15 mother      2     1         2
#16 father      2     3         3
#17 mother      1     2         1
#18 father      5     2         4
#19 mother      5     4         4
#20 father      1     4         2

输出值与预期不同,因为 OP 在构建输入示例时使用了 sample 而没有 set.seed


对于编辑过的部分,我们切换了 names_to 并更改了 names_sep 正则表达式环视切换

# // change the column names by rearranging the 'm|f'
# // at the end of the column name
names(toydat)[-1] <- sub("^(.)(.*)","\\2\\1",names(toydat)[-1]) 
toydat %>% 
   select(-id) %>% 
   pivot_longer(cols = everything(),names_to = c(".value","gender"),names_sep = "(?<=[a-z])(?=[mf]$)") %>%
       mutate(gender = case_when(gender == 'm' ~ 'mother',TRUE ~ 'father'))

-输出

# A tibble: 20 x 4
#   gender income   edu ethnicity
#   <chr>   <int> <int>     <int>
# 1 mother      1     2         1
# 2 father      5     5         1
# 3 mother      5     4         3
# 4 father      4     4         2
# 5 mother      3     3         4
# 6 father      2     2         4
# 7 mother      4     5         2
# 8 father      3     1         3
# 9 mother      2     1         5
#10 father      1     3         5
#11 mother      1     2         1
#12 father      5     5         1
#13 mother      5     4         3
#14 father      4     4         2
#15 mother      3     3         4
#16 father      2     2         4
#17 mother      4     5         2
#18 father      3     1         3
#19 mother      2     1         5
#20 father      1     3         5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。