如何unite()并在数据框中显示具有重要性值的asterix

时间:2017-08-03 12:27:15

标签: r dplyr

我有这个df,一列中有很多系数,下一列中有星号。我试图联合()两列,但我得到一个错误。我举了一个例子:

library(dplyr)
# creating df
df <- tibble(city = c("amsterdam", "amsterdam", "amsterdam", "rotterdam", "rotterdam", "rotterdam"),
                terms = c("intercept", "food", "service", "intercept", "food", "service"),
                coefficient = c(23, .54, -.02, 18, .86, .67),
                p.value = c("***", "*", " ", "***", "***", "**"))

# Here i try to unite the two columns
df %>% 
  mutate(coefficient = unite(coefficient, p.value, sep = ""))

当我运行此操作时,我收到以下错误消息:

Error in mutate_impl(.data, dots) : 
  Evaluation error: no applicable method 
for 'unite_' applied to an object of class 
"c('double', 'numeric')".

谁能告诉我如何解决这个问题?如果可能的话,如果最终的列以这样的方式对齐,即减号的开头早于最后一列中的数字,那就太好了。

2 个答案:

答案 0 :(得分:2)

documentation我看到unite是一个“将多个列粘贴到一个列中的便利功能”。所以我假设您只是想将列粘贴在一起?在这种情况下,您可以这样做:

df %>% 
  mutate(coefficient = paste0(coefficient, p.value))

       city     terms coefficient p.value
      <chr>     <chr>       <chr>   <chr>
1 amsterdam intercept       23***     ***
2 amsterdam      food       0.54*       *
3 amsterdam   service      -0.02         
4 rotterdam intercept       18***     ***
5 rotterdam      food     0.86***     ***
6 rotterdam   service      0.67**      **

答案 1 :(得分:2)

unite旨在用作独立函数,它期望数据帧作为其第一个参数,并返回另一个数据帧(不是mutate期待的)请参阅{{ 1}}:

?unite