[英]Using as.numeric, with functions and pipes in R
I have a function that looks like this 我有一个看起来像这样的功能
calc_df <- function(A_df, B_df){
C_df <- filter(A_df, Type == "Animal") %>%
left_join(B_df) %>%
as.numeric(C$Count)
Where I cannot get the last lime to work, the first 3 work properly, but I would like the last line to take the column "Count" from the new df calculated in the function and make it numeric. 在我无法完成最后工作的情况下,前3个工作正常,但我希望最后一行从函数中计算的新df中取“Count”列并使其成为数字。 (Right now it is a character vector)
(现在它是一个字符向量)
** I have to do this at the end of the function because before the filter command, the Count column contains letters and cannot be made as.numeric **我必须在函数结束时执行此操作,因为在filter命令之前,Count列包含字母,不能将其作为.numeric
Looks like you're using dplyr
, and that you want to change or add a column. 看起来您正在使用
dplyr
,并且您想要更改或添加列。 This is what the dplyr::mutate
function does. 这就是
dplyr::mutate
函数的作用。
Replace 更换
as.numeric(C$Count)
with 同
mutate(Count = as.numeric(Count))
to replace the old, non-numeric Count
column with the coerced-to-numeric replacement. 使用强制到数字替换替换旧的非数字
Count
列。
As to why your code didn't work, there are a few problems: 至于为什么你的代码不起作用,有一些问题:
dplyr
is made for working with data frames, and the main dplyr functions (select, filter, mutate, summarize, group_by, *_join, ...) expect data frames as the first argument, and then return data frames. dplyr
用于处理数据帧,主要的dplyr函数(select,filter,mutate,summarize,group_by,* _join,...)期望数据帧作为第一个参数,然后返回数据帧。 By piping the result of a left join into as.numeric
, you are really calling as.numeric(unnamed_data_frame_from_your_join, C$Count)
, which clearly doesn't make much sense. 通过将左连接的结果
as.numeric
给as.numeric
,您实际上调用as.numeric(unnamed_data_frame_from_your_join, C$Count)
,这显然没有多大意义。
You are trying to reference a data frame called C
inside a definition for a data frame called C_df
, which I think you mean to be the same thing. 您正在尝试在名为
C_df
的数据框的定义中引用名为C
的数据框,我认为您的意思是相同的。 There's two issues here: (1) the mismatch between the names C
and C_df
, and (2) you can't reference C_df
inside it's own definition. 这里有两个问题:(1)名称
C
和C_df
之间的不匹配,以及(2)你不能在它自己的定义中引用C_df
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.