使用dplyr mutate自动生成新变量名

时间:2016-07-12 23:03:59

标签: r dplyr

我想在使用dplyr时动态创建变量名;虽然,我也可以使用非dplyr解决方案。

例如:

data(iris)
library(dplyr) 

iris <- iris %>%
  group_by(Species) %>%
  mutate(
    lag_Sepal.Length = lag(Sepal.Length),
    lag_Sepal.Width  = lag(Sepal.Width),
    lag_Petal.Length = lag(Petal.Length)
  ) %>%
  ungroup

head(iris)

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species lag_Sepal.Length lag_Sepal.Width
             (dbl)       (dbl)        (dbl)       (dbl)  (fctr)            (dbl)           (dbl)
    1          5.1         3.5          1.4         0.2  setosa               NA              NA
    2          4.9         3.0          1.4         0.2  setosa              5.1             3.5
    3          4.7         3.2          1.3         0.2  setosa              4.9             3.0
    4          4.6         3.1          1.5         0.2  setosa              4.7             3.2
    5          5.0         3.6          1.4         0.2  setosa              4.6             3.1
    6          5.4         3.9          1.7         0.4  setosa              5.0             3.6
    Variables not shown: lag_Petal.Length (dbl)

但是,我想创建100个这样的“滞后”变量,而不是这样做三次,这些变量的名称为:lag_original variable name。我试图找出如何在不输入新变量名100次的情况下做到这一点,但我的时间很短。

我在SO的其他地方查看了this示例和this示例。它们是相似的,但我不能完全拼凑我需要的具体解决方案。任何帮助表示赞赏!

修改
感谢@BenFasoli的灵感。我接受了他的回答并稍微调整了一下以获得我需要的解决方案。 我还使用了This RStudio BlogThis SO post。变量名中的“滞后”是尾随而不是前导,但我可以忍受。

我的最终代码会发布在此处以防其他人:

lagged <- iris %>%
  group_by(Species) %>%
  mutate_at(
    vars(Sepal.Length:Petal.Length),
    funs("lag" = lag)) %>%
  ungroup

# A tibble: 6 x 8
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_lag Sepal.Width_lag
         <dbl>       <dbl>        <dbl>       <dbl>  <fctr>            <dbl>           <dbl>
1          5.1         3.5          1.4         0.2  setosa               NA              NA
2          4.9         3.0          1.4         0.2  setosa              5.1             3.5
3          4.7         3.2          1.3         0.2  setosa              4.9             3.0
4          4.6         3.1          1.5         0.2  setosa              4.7             3.2
5          5.0         3.6          1.4         0.2  setosa              4.6             3.1
6          5.4         3.9          1.7         0.4  setosa              5.0             3.6
# ... with 1 more variables: Petal.Length_lag <dbl>

3 个答案:

答案 0 :(得分:3)

您可以使用mutate_all(或mutate_at用于特定列),然后将lag_添加到列名称中。

data(iris)
library(dplyr) 

lag_iris <- iris %>%
  group_by(Species) %>%
  mutate_all(funs(lag(.))) %>%
  ungroup
colnames(lag_iris) <- paste0('lag_', colnames(lag_iris))

head(lag_iris)

  lag_Sepal.Length lag_Sepal.Width lag_Petal.Length lag_Petal.Width lag_Species
             <dbl>           <dbl>            <dbl>           <dbl>      <fctr>
1               NA              NA               NA              NA      setosa
2              5.1             3.5              1.4             0.2      setosa
3              4.9             3.0              1.4             0.2      setosa
4              4.7             3.2              1.3             0.2      setosa
5              4.6             3.1              1.5             0.2      setosa
6              5.0             3.6              1.4             0.2      setosa

答案 1 :(得分:2)

这是一个data.table方法。在这种情况下,我选择了带数字的列。您要做的是提前选择列名并创建新列名。然后,您将dplyr包中的shift()lag()的{​​{1}}应用于您选择的每个列。

lead()

答案 2 :(得分:1)

既然你对非dplyr感到满意,试试这个:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_rectangle_switches, container, false);

    ButterKnife.bind(this, rootView);

    // Note: Title set by AmbientPagerFragment

    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);

    adapter = new SwitchAdapter(switchList, dimmableList, ghostList, editMode, bus);
    recyclerView.setAdapter(adapter);

    displaySwitches();
    return rootView;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icon_edit:
            editMode = true;
            changeEditMode();
            break;
        case R.id.icon_cancel:
            editMode = false;
            changeEditMode();
            break;
    }
    return super.onOptionsItemSelected(item);
}

public void changeEditMode() {
    bus.post(new EditModeEvent(editMode));

    reattachFragment();

    displaySwitches();
    getActivity().invalidateOptionsMenu();
}

private void displaySwitches() {

    if (editMode) {
        callback = new ItemTouchCallback((ItemTouchHelperAdapter) adapter);
        mTouchHelper = new ItemTouchHelper(callback);
        mTouchHelper.attachToRecyclerView(recyclerView);
        }
    }

private void reattachFragment(){
    ft = getFragmentManager().beginTransaction();
    ft.detach(this).attach(this).commit();
}
相关问题