【问题标题】:Tibble and paste functionTibble 和粘贴功能
【发布时间】:2020-11-18 23:30:50
【问题描述】:

我尝试在小标题上使用粘贴功能,但我在下面得到了这个结果。我试图用 as.data.frame 对其进行转换,但结果是一样的。我真的需要使用 dplyr 来管理更大的数据集。

提前致谢。

Species=iris %>% select(Species)
paste("This is a ",Species)

[1] "这是一个 c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 , 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 , 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 , 3, 3, 3, 3, 3)"

【问题讨论】:

  • 你到底想做什么?可能你需要Species=iris %>% pull(Species) 而不是select
  • paste('This is a', iris$Species) ?

标签: r dataframe tidyverse paste tibble


【解决方案1】:

这是我必须努力的解决方案。您可以使用 mutate 将字符串“This is a”与 Species 列粘贴在一起。我已将其保存为名为 which_species 的变量。

# Load tidyverse
library(tidyverse)

# Select Species from iris 
Species = iris %>% select(Species)
 
# I created a new variable pasting your string and the Species column together
Species <- Species %>% mutate(which_species = paste0("This is a ", Species))

# Check out the output
head(Species)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-07
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    相关资源
    最近更新 更多