首页 > 解决方案 > 努力使用 Dplyr 进行变异

问题描述

我对 R 还是很陌生,在尝试获取平均值时遇到了以下错误:

Error: Problem with `mutate()` input `inflight_exp`. x error in evaluating the argument 'x' in selecting a method for function 'rowMeans': unused argument (starts_with("Inflight")) i Input `inflight_exp` is `rowMeans(fal1 %>% select(starts_with("Inflight")))`. Backtrace: 9. dplyr::mutate(...) 11. dplyr:::mutate_cols(.data, ...)

我已经对在 Internet 上找到的一些代码进行了逆向工程,但我看不出他们的代码和我的代码之间的区别 - 请有人帮助并指导我做错了什么?

我采取的步骤如下:将数据拆分为 3 个元素:

  1. 在线是与在线体验相关的标准
  2. 机场是与飞行前经历的因素有关的标准
  3. 机上是与机上体验相关的满意度标准
fal1 = rename(fal, Inflight1 = seat_comfort, 
    Airport1 = departure_arrival_time_convenient,
    Inflight2 = food_drink, 
    Airport2 = gate_location,
    Inflight3 = inflightwifi_service,
    Inflight4 = inflight_entertainment,
    Online1 = online_support,
    Online2 = ease_of_onlinebooking,
    Inflight5 = onboard_service,
    Inflight6 = leg_room_service,
    Airport3 = baggage_handling,
    Airport4 = checkin_service,
    Inflight7 = cleanliness,
    Online3 = online_boarding)
fal1 = fal1 %>% 
    mutate(inflight_exp = rowMeans(fal1 %>% select(starts_with("Inflight"))),
        airport_exp = rowMeans(fal1 %>% select(starts_with("Airport"))),
        online_exp = rowMeans(fal1 %>% select(starts_with("Online"))),
        male = ifelse(gender == 1,0,1))

标签: rdplyr

解决方案


推荐阅读