尝试选择R中的列,但仅假设行

问题描述

我正在尝试从827个现有变量中选择41个变量。我正在使用代码

private final void transfer(Node<K,V>[] tab,Node<K,V>[] nextTab) {
        int n = tab.length,stride;
        //...
        int nextn = nextTab.length;
        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
        boolean advance = true;
        boolean finishing = false; // to ensure sweep before committing nextTab
        for (int i = 0,bound = 0;;) {
            Node<K,V> f; int fh;
            while (advance) {
                int nextIndex,nextBound;
                if (--i >= bound || finishing)
                    advance = false;
                else if ((nextIndex = transferIndex) <= 0) {
                    i = -1;
                    advance = false;
                }
                else if (U.compareAndSwapInt
                         (this,TRANSFERINDEX,nextIndex,nextBound = (nextIndex > stride ?
                                       nextIndex - stride : 0))) {
                    bound = nextBound;
                    i = nextIndex - 1;
                    advance = false;
                }
            }
            if (i < 0 || i >= n || i + n >= nextn) {
                int sc;
                if (finishing) {
                    nextTable = null;
                    table = nextTab;
                    sizeCtl = (n << 1) - (n >>> 1);
                    return;
                }
                if (U.compareAndSwapInt(this,SIZECTL,sc = sizeCtl,sc - 1)) {
                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
                        return;
                    finishing = advance = true;
                    i = n; // recheck before commit
                }
            }
            //...
}

此后,我收到错误消息:

`myvars <- c('newid',# CU id 'region',# region 'state',# state 'cutenure',# housing tenure 'fam_size',# family size 'no_earnr',# number of earners 'num_auto',# number of vehicles owned 'popsize',# population size (see codes) 'inclass',# income class 'age_ref',# age reference person 'educ_ref',# education reference person (see codes) 'ref_race',# race reference person (1=white,2=black,3= nat-am,4=asian,5=pac-isl,6=multi-race) 'inc_hrs1',# hours x week by ref person 'inc_hrs2',# hours x week by spouse 'incweek1',# number weeks worked ref person 'incweek2',# number weeks worked spouse 'fincbtax',# income before tax past 12 month 'fincatax',# income after tax past 12 month 'fsalaryx',# wage and salary income before ded. 'totexppq',# tot exp prev quarter 'totexpcq',# tot exp curr quarter 'majapppq',# major appliances prev quarter 'majappcq',# major appliances curr quarter 'FOODHOME',# Expenditures food at home 'FOODAWAY',# Food away from home 'ALCBEV',# Alcholic Beverages 'OWNDWECQ',# Owned Dwellings 'ZRENTDWL',# Rented Dwellings 'OTHLODCQ',# Other Lodging 'UTILCQ',# Utilities 'MISCEQPQ',# Household Equipment 'HOUSOPCQ',# Household Operations 'APPARCQ',# Apparel and Services 'VEHICLCQ',# Vehicle Expenditures 'OTHVEHCQ',# Other Vehicle Expenditures 'GASMOCQ',# Gasoline 'TRNOTHCQ',# Public Transportation 'HEALTHCQ',# Health Care 'ENTERTCQ',# Entertainment 'PERSCACQ',# Personal Care 'READCQ',# Reading 'EDUCACQ',# Education 'TOBACCCQ' # Tobacco ) newdataQ1 = dataQ1[,myvars]` (dataQ1,,myvars)中的错误:未定义的列已选中 追溯:

  1. dataQ1 [,myvars]
  2. [.data.frame(dataQ1,,myvars)
  3. 停止(“选择未定义的列”)

如果我更改逗号并放入

newdataQ1 = dataQ1 [myvars,]

它允许我继续操作,但保留41行而不是41列,保持原始列数。

我该如何解决

谢谢。

解决方法

也许dplyr是您分区数据的好方法。 这是一个很棒的程序包,使您的请求非常容易且易于阅读 (我正在使用管道运算符,它们也使工作变得更轻松,代码更易读)

newdataQ1 <- dataQ1 %>% select(myvars)

值得查看dplyr的教程(例如在datacamp上)以熟悉语法。

如果只想使用基本R,则需要指定要选择的名字:

newdataQ1 <- dataQ1[,colnames(dataQ1) %in% myvars]