R - 构造具有多个列而没有赋值的数据框

时间:2016-11-25 15:59:14

标签: r dataframe multiple-columns

我的目标是创建一个i,其中包含ColumnName_1个名为ColumnName_i的列i = 3 - > structure(list(ColumnName_1 = c(0, 0, 0), ColumnName_2 = c(0, 0, 0), ColumnName_3 = c(0, 0, 0)), .Names = c("ColumnName_1", "ColumnName_2", "ColumnName_3"), row.names = c(NA, -3L), class = "data.frame")

结果将是assign

        import android.content.Intent;
        import android.content.SharedPreferences;
        import android.os.Bundle;
        import android.support.design.widget.FloatingActionButton;
        import android.support.design.widget.Snackbar;
        import android.support.v4.app.Fragment;
        import android.support.v4.app.FragmentManager;
        import android.support.v4.content.ContextCompat;
        import android.view.View;
        import android.support.design.widget.NavigationView;
        import android.support.v4.view.GravityCompat;
        import android.support.v4.widget.DrawerLayout;
        import android.support.v7.app.ActionBarDrawerToggle;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.Toolbar;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.Window;
        import android.view.WindowManager;
        import android.widget.ArrayAdapter;
        import android.widget.ImageButton;
        import android.widget.ImageView;
        import android.widget.ListView;
        import android.widget.TextView;

public class Manager extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener,View.OnClickListener {
    Data dat=new Data();
    private ImageView imageView;
    private ImageButton logout,viewlibrary;
    TextView headerTitle;
    FragmentManager fragmentManager;
    String manager="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);
        headerTitle=(TextView)findViewById(R.id.headertitle);
        headerTitle.setText("Manager's Panel");
        TextView tv=(TextView)findViewById(R.id.usrmsgman);
        tv.setText("USER : "+dat.getUser());
        logout=(ImageButton)findViewById(R.id.logoutbut);
        viewlibrary=(ImageButton)findViewById(R.id.libview);
/*
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        */
        ListView lv=(ListView)findViewById(R.id.listView);
        Downloader d=new Downloader(this,dat.getHost()+"managerhome.php",lv);
        d.execute();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------------------------------------------------------------------
What to do here?
---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------------------------------------------------------------------


*/
            });

            logout.setOnClickListener(this);
            viewlibrary.setOnClickListener(this);
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.logoutbut :
                    SharedPreferences sp=getSharedPreferences("Login", 0);
                    SharedPreferences.Editor Ed=sp.edit();
                    Ed.putString("Unm",null );
                    Ed.putString("Psw",null);
                    Ed.putString("app",null);
                    Ed.commit();
                    this.finish();
                    startActivity(new Intent(Manager.this,Login.class));
                    break;
                case R.id.libview :
                    startActivity(new Intent(this,ViewLibrary.class));
                    break;
            }
        }

        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }
    public void removeFragment(Fragment fragment){
        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_issue) {
            IssueFragment issueFragment=new IssueFragment();
            FragmentManager manager=getSupportFragmentManager();
            fragmentManager=manager;
            manager.beginTransaction().replace(R.id.reloutForFragment,issueFragment).commit();
        } else if (id == R.id.nav_reissue) {
            RIssueFragment rissueFragment = new RIssueFragment();
            FragmentManager manager=getSupportFragmentManager();
            fragmentManager=manager;
            manager.beginTransaction().replace(R.id.reloutForFragment, rissueFragment).commit();

        } else if (id == R.id.nav_retbook) {
            ReturnFragment returnFragment= new ReturnFragment();
            FragmentManager manager=getSupportFragmentManager();
            fragmentManager=manager;
            manager.beginTransaction().replace(R.id.reloutForFragment,returnFragment).commit();

        } else if (id == R.id.nav_search) {
            SearchFragment searchFragment= new SearchFragment();
            FragmentManager manager=getSupportFragmentManager();
            fragmentManager=manager;
            manager.beginTransaction().replace(R.id.reloutForFragment,searchFragment).commit();

        } else if (id == R.id.nav_abtApp) {
            AbtAppFragment abtAppFragment=new AbtAppFragment();
            FragmentManager manager=getSupportFragmentManager();
            fragmentManager=manager;
            manager.beginTransaction().replace(R.id.reloutForFragment,abtAppFragment).commit();

        } else if (id == R.id.nav_abtDev) {
            AbtDevFragment abtDevFragment= new AbtDevFragment();
            FragmentManager manager=getSupportFragmentManager();
            fragmentManager=manager;
            manager.beginTransaction().replace(R.id.reloutForFragment,abtDevFragment).commit();

        } else if (id == R.id.nav_view_mem) {
            ViewMemberFragment viewMemberFragment= new ViewMemberFragment();
            FragmentManager manager=getSupportFragmentManager();
            fragmentManager=manager;
            manager.beginTransaction().replace(R.id.reloutForFragment,viewMemberFragment).commit();

        } else if (id == R.id.nav_log) {
            LogFragment logFragment= new LogFragment();
            FragmentManager manager=getSupportFragmentManager();
            fragmentManager=manager;
            manager.beginTransaction().replace(R.id.reloutForFragment,logFragment).commit();

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

我从其他问题中了解到其他主题不建议使用Intent intent = getIntent(); finish(); startActivity(intent); ,但这是我目前看到的唯一解决方案。

3 个答案:

答案 0 :(得分:1)

这是一种方法,它使用replicatedata.frame构建一个空数据框。无论你想要什么尺寸(使用数字变量),然后使用setNames来提供变量名。

setNames(data.frame(replicate(3, numeric(3), simplify=FALSE)),
         paste0("colName", seq_len(3)))

  colName1 colName2 colName3
1        0        0        0
2        0        0        0
3        0        0        0

复制的第一个参数提供列数,第二个参数numeric(3)提供行数。

答案 1 :(得分:1)

我们可以使用以下内容,假设您需要m行和n列:

data.frame(matrix(0, nrow = m, ncol = n,
           dimnames = list(NULL, paste0("ColumnName_", 1:n))) )

所有其他答案都使用循环来创建列,而此解决方案是完全矢量化的。

答案 2 :(得分:1)

lapply的版本。

i <- 3
setNames(as.data.frame(lapply(1:i, function(k) c(0, 0, 0))),
         paste("ColumnName_", 1:i, sep = ""))