“警告:太多(很少)值"在 R 中使用 tidyr 包 [英] "Warning: too many (few) values" for using tidyr packages in R

查看:27
本文介绍了“警告:太多(很少)值"在 R 中使用 tidyr 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据集D7

name sex_age eye_color height
1    J    M.34     Other     61
2    A    F.55      Blue     59
3    T    M.76     Brown     51
4    D    F.19     Other     57

我想把sex_age列分成sex列和age列,所以我输入

I want to separate the column sex_age into sex column and age column, so I type

separate(D7,sex_age,c('sex','age'),sep='.')

但它会产生

name sex age eye_color height
1    J             Other     61
2    A              Blue     59
3    T             Brown     51
4    D             Other     57
Warning message:
Too many values at 4 locations: 1, 2, 3, 4 

此外,当我将原始数据集 D7 修改为 D8

Also, when I modify my original data set D7 into D8

name sex_age eye_color height
1    J    M_34     Other     61
2    A    F_55      Blue     59
3    T    M_76     Brown     51
4    D    F_19     Other     57

然后我输入 D7 %>% separate(sex_age,c('sex','age'),sep="_") 它给出了

And I type D7 %>% separate(sex_age,c('sex','age'),sep="_") it gives

name  sex  age eye_color height
1    J M.34 <NA>     Other     61
2    A F.55 <NA>      Blue     59
3    T M.76 <NA>     Brown     51
4    D F.19 <NA>     Other     57
Warning message:
Too few values at 4 locations: 1, 2, 3, 4 

我是否误用了 separate 功能?我很困惑.感谢您的任何建议.

Did I misuse the separate function? I am very puzzled. Thank you for any suggestions.

推荐答案

因为 sep= 参数考虑了正则表达式和 . 是一个特殊字符,因此我们需要有 \\ 在这样的特殊字符之前,以便将它们作为普通字符读取

since sep= argument considers regex and . is a special character, therefore we need to have \\ before such special characters such that they are read as normal characters

separate(df, sex_age, into = c("sex", "age"), sep = "\\.")

这篇关于“警告:太多(很少)值"在 R 中使用 tidyr 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆