R-dplyr select()和filter()

时间:2018-09-03 09:52:43

标签: r select dplyr

我是R的相对较新的用户,正在尝试过滤掉相关变量,并忽略var cards = document.querySelectorAll(".card"); Array.from(cards).forEach(card => { card.addEventListener("click", (event) => { console.log(event.target.querySelector(".date").innerHTML); console.log(event.target.querySelector(".length").innerHTML); console.log(event.target.querySelector(".type").innerHTML); console.log(event.target.querySelector(".comment").innerHTML); }); }); 来制作一些图形。当我使用NA函数时,出现以下错误消息:

  

没有适用于“ select_”的适用于类“ c('integer','numeric')”的对象的方法

我正在使用的代码是这样:

select()

怎么了?

1 个答案:

答案 0 :(得分:0)

您需要使用管道运算符-%>%将输入数据帧“推”到'dlyr`包的select处理函数中。请参见下面的仿真代码:

library(dplyr)

# data simulation
set.seed(123)
df <- data.frame(
  year = 2011:2018, 
  Total_US_received = (1:8) * 100, 
  Total_US_required = (1:8) * 50,
  no_need1 = rnorm(8),
  no_need2 = rnorm(8)
)

head(df)
#  year Total_US_received Total_US_required    no_need1   no_need2
# 1 2011               100                50 -0.56047565 -0.6868529
# 2 2012               200               100 -0.23017749 -0.4456620
# 3 2013               300               150  1.55870831  1.2240818
# 4 2014               400               200  0.07050839  0.3598138
# 5 2015               500               250  0.12928774  0.4007715
# 6 2016               600               300  1.71506499  0.1106827


# this is how select works in dplyr
df_out <- df %>% select(year, Total_US_received, Total_US_required)
head(df_out)

输出:

  year Total_US_received Total_US_required
1 2011               100                50
2 2012               200               100
3 2013               300               150
4 2014               400               200
5 2015               500               250
6 2016               600               300