我有与此类似的代码,其中包含用于创建数据框“ full_tb”的管道,该管道失败,因为倒数第二行(产生ID列的突变)正在调用尚未创建的对象(“ full_tb”)
library(random)
library(dplyr)
set.seed(1)
Codes <- as.vector(randomStrings(n = 10, len = 3, digits = TRUE, upperalpha = FALSE,
unique = TRUE))
frame1 <- data.frame(
A = sort(Codes),
B = sample(x = c("Tree", "Shrub", "Fern"), size = 10, replace = TRUE))
)
frame2 <- data.frame(
Row_no = sort(sample(x = 1:10)),
C = sample(x = sample(x = c("Tree", "Shrub", "Fern"), size = 30, replace = TRUE))
)
# Here is where the problem begins
full_tb <- frame1 %>% mutate(Row_no = as.numeric(rownames(frame1))) %>%
inner_join(frame2) %>%
mutate(ID = as.numeric(rownames(full_tb))) %>%
select(ID, A, B, C)
# Joining by = "Row_no"
# Error in mutate_impl(.data, dots):
# Evaluation error: object 'full_tb' not found
但是,如果我将管道分成两个块,则运行正常。
full_tb <- frame1 %>% mutate(Row_no = as.numeric(rownames(frame1))) %>%
inner_join(frame2)
# Joining by = "Row_no"
full_tb <- full_tb %>% mutate(ID = as.numeric(rownames(full_tb))) %>%
select(ID, A, B, C)
是否存在一种解决方法,可以将所有内容都管道化为一个块,而不必将第一个代码块分为两部分?
答案 0 :(得分:0)
通过在要传递的代码块中添加点作为行名的参数,在连接后为整个数据帧生成ID。因此,无需在行名中指定数据框的名称。
full_tb <- frame1 %>% mutate(Row_no = as.numeric(rownames(frame1))) %>%
inner_join(frame2) %>%
mutate(ID = as.numeric(rownames(.))) %>%
select(ID, A, B, C)