如何在列表中使用mutate?

时间:2016-06-22 18:37:22

标签: r dplyr

改变列表的正确方法是什么?在这种特定情况下,列表由split

返回
library(dplyr)
csv<-data.frame(participant_number=c(1,1,1,2,2),SelfEsteem=c(3,4,2,1,3))
csv<-csv%>%split(.,.$participant_number)%>%mutate(.,var(.$SelfEsteem))

错误:

Error in UseMethod("mutate_") : 
  no applicable method for 'mutate_' applied to an object of class "list"

更新回复评论:我打算计算每个组SelfEsteem的方差(分组变量为participant_number)。如果我尝试group_by我没有得到预期的结果....为什么?

library(dplyr)

#Please note I changed the dataframe to make my point about variance differences more obvious
csv<-data.frame(participant_number=c(1,1,1,2,2),SelfEsteem=c(3,4,2,1,3))

csv<-csv%>%group_by(participant_number)%>%mutate(.,SE_variance=var(.$SelfEsteem))

#var(c(3,4,2)) #1
#var(c(1,3))  #2

预期:

        participant_number SelfEsteem SE_variance
(dbl)            (dbl)       (dbl)
1                  1          3         1
2                  1          4         1
3                  1          2         1
4                  2          1         2
5                  2          3         2

2 个答案:

答案 0 :(得分:3)

group_by的尝试失败,因为您覆盖了mutate的搜索路径。 Mutate使用非标准评估,因此它将首先在其data参数的列中查找变量。

使用竖线(%>%)时,点.表示整个数据框.$SelfEsteem表示整个< / em>来自整个数据框的SelfEsteem列。

您只需要简化一点(不要覆盖默认值)以获得预期的结果。

csv %>% 
  group_by(participant_number) %>%
  mutate(SE_variance = var(SelfEsteem))
# Source: local data frame [5 x 3]
# Groups: participant_number [2]
# 
#   participant_number SelfEsteem SE_variance
#                (dbl)      (dbl)       (dbl)
# 1                  1          3           1
# 2                  1          4           1
# 3                  1          2           1
# 4                  2          1           2
# 5                  2          3           2

答案 1 :(得分:2)

如果您确实需要使用list个对象,可以使用mutate list maplibrary(dplyr) library(purr) csv %>% split(.,.$participant_number) # your list map(~mutate(.,var(.$SelfEsteem))) # this will mutate a list

bind_rows()

现在,您可以将所有内容与csv %>% split(.,.$participant_number) %>% map(~mutate(.,var(.$SelfEsteem))) %>% bind_rows()

放在一起
public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private SessionManager session;
private SQLiteHandler db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // SqLite database handler
    db = new SQLiteHandler(getApplicationContext());
    // session manager
    session = new SessionManager(getApplicationContext());

    if (!session.isLoggedIn()) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    } else {

        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_one)));
        tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_two)));
        tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_three)));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 1) {

// call your sent email request and stored that in db after that in your fragment fetch the content from db and display it on ui
}
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

你得到了你需要的东西。