"Warning: too many (few) values" 在 R 中使用 tidyr 包

"Warning: too many (few) values" for using tidyr packages in R

我有以下数据集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 列,所以我输入

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

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="_") 它给出

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 功能?我很不解。感谢您的任何建议。

因为 sep= 参数考虑正则表达式并且 . 是一个特殊字符,因此我们需要在这些特殊字符之前加上 \ 以便它们被读取为普通字符

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