如何仅选择值大于0的属性

时间:2014-07-09 17:27:41

标签: r

   f<-function(data)
   + {
   +     braz<-read.csv("braz.csv",header=T)
   +     index<-1:nrow(braz)
   +     tindex<-sample(index,trunc(length(index)*.7))
   +     trainbraz<-braz[tindex,]
    +     testbraz<-braz[-tindex,]
   +     weights<-chi.squared(Class~.,trainbraz)
    +     return(weights)
   }
    f()   #after running the function the require result is shown below. i want a code to be inserted in this function which will give me only values greater than 0



    attr_importance
Word1         0.0000000
Word2         0.0000000
Word3         0.0000000
Word4         0.0000000
Word5         0.0000000
Word6         0.0000000
Word7         0.4224098
Word8         0.0000000
Word9         0.0000000
Word10        0.0000000
Word11        0.3544805
Word12        0.2957039
Word13        0.0000000
Word14        0.0000000
Word15        0.0000000
Word16        0.0000000
Word17        0.0000000
Word18        0.0000000
Word19        0.0000000
Word20        0.0000000
Word21        0.0000000
Word22        0.0000000
Word23        0.0000000
Word24        0.0000000
Word25        0.0000000
Word26        0.0000000
Word27        0.0000000
Word28        0.0000000
Word29        0.0000000
Word30        0.2886572
Word31        0.0000000
Word32        0.0000000
Word33        0.0000000
Word34        0.0000000
Word35        0.0000000
Word36        0.0000000
Word37        0.0000000
Word38        0.0000000
Word39        0.0000000
Word40        0.0000000
Word41        0.0000000
Word42        0.0000000
Word43        0.0000000
Word44        0.0000000
Word45        0.0000000
Word46        0.0000000
Word47        0.0000000
Word48        0.0000000
Word49        0.0000000
Word50        0.0000000
Word51        0.0000000
Word52        0.0000000
Word53        0.0000000
Word54        0.0000000
Word55        0.2693569
Word56        0.0000000
Word57        0.0000000
Word58        0.0000000
Word59        0.0000000
Word60        0.0000000
Word61        0.0000000
Word62        0.0000000
Word63        0.3416860
Word64        0.0000000
Word65        0.0000000
Word66        0.0000000
Word67        0.0000000
Word68        0.0000000
Word69        0.0000000
Word70        0.0000000
Word71        0.0000000
Word72        0.0000000
Word73        0.3355631
Word74        0.0000000
Word75        0.0000000
Word76        0.3353073
Word77        0.3311813
Word78        0.0000000
Word79        0.0000000
Word80        0.0000000
Word81        0.0000000
Word82        0.0000000
Word83        0.0000000
Word84        0.0000000
Word85        0.0000000
Word86        0.0000000
Word87        0.0000000
Word88        0.0000000
Word89        0.0000000
Word90        0.0000000
Word91        0.3231609
Word92        0.2886572
Word93        0.0000000
Word94        0.0000000
Word95        0.0000000
Word96        0.0000000
Word97        0.0000000
Word98        0.0000000
Word99        0.0000000
Word100       0.0000000
Word101       0.0000000
Word102       0.0000000
Word103       0.0000000
Word104       0.0000000
Word105       0.3919976
Word106       0.0000000
Word107       0.3592106
Word108       0.0000000
Word109       0.0000000
Word110       0.0000000
Word111       0.0000000
Word112       0.0000000
Word113       0.0000000
Word114       0.0000000
Word115       0.0000000
Word116       0.0000000
Word117       0.0000000
Word118       0.0000000
Word119       0.0000000
Word120       0.0000000
Word121       0.0000000
Word122       0.0000000
Word123       0.2693569
Word124       0.0000000
Word125       0.0000000
Word126       0.2886572
Word127       0.4289901
Word128       0.0000000
Word129       0.2852853
Word130       0.0000000
Word131       0.0000000
Word132       0.0000000
Word133       0.3125491
Word134       0.0000000
Word135       0.0000000
Word136       0.0000000
Word137       0.0000000
Word138       0.0000000
Word139       0.0000000
Word140       0.0000000
Word141       0.0000000
Word142       0.0000000
Word143       0.0000000
Word144       0.0000000
Word145       0.0000000
Word146       0.0000000
Word147       0.0000000
Word148       0.0000000
Word149       0.0000000
Word150       0.2625822
Word151       0.4233411
Word152       0.3391614
Word153       0.0000000
Word154       0.0000000
Word155       0.0000000
Word156       0.0000000
Word157       0.0000000
Word158       0.0000000
Word159       0.0000000
Word160       0.0000000
Word161       0.0000000
Word162       0.0000000
Word163       0.0000000
Word164       0.0000000
Word165       0.0000000
Word166       0.0000000
Word167       0.0000000
Word168       0.0000000
Word169       0.0000000
Word170       0.3093441
Word171       0.0000000
Word172       0.0000000
Word173       0.0000000
Word174       0.0000000
Word175       0.0000000
Word176       0.0000000
Word177       0.0000000
Word178       0.0000000
Word179       0.0000000
Word180       0.0000000
Word181       0.0000000
Word182       0.0000000
Word183       0.0000000
Word184       0.0000000
Word185       0.0000000
Word186       0.0000000
Word187       0.0000000
Word188       0.0000000
Word189       0.0000000
Word190       0.0000000
Word191       0.7875438
Word192       0.0000000
Word193       0.0000000
Word194       0.0000000
Word195       0.0000000
Word196       0.0000000
Word197       0.0000000
Word198       0.0000000
Word199       0.3166195
Word200       0.0000000
Word201       0.2886572
Word202       0.6386000
Word203       0.0000000
Word204       0.0000000
Word205       0.0000000
Word206       0.0000000
Word207       0.7760143
Word208       0.0000000
Word209       0.0000000
Word210       0.0000000
Word211       0.6881407
Word212       0.0000000
Word213       0.0000000
Word214       0.0000000
Word215       0.0000000
Word216       0.5042257
Word217       0.0000000
Word218       0.0000000
Word219       0.0000000
Word220       0.0000000
Word221       0.0000000
Word222       0.0000000
Word223       0.0000000
Word224       0.0000000
Word225       0.0000000
Word226       0.0000000
Word227       0.0000000
Word228       0.0000000
Word229       0.0000000
Word230       0.0000000
Word231       0.0000000
Word232       0.0000000
Word233       0.0000000
Word234       0.2861000
Word235       0.0000000
Word236       0.0000000
Word237       0.0000000
Word238       0.0000000
Word239       0.0000000
Word240       0.0000000
Word241       0.0000000
Word242       0.0000000
Word243       0.0000000
Word244       0.0000000
Word245       0.0000000
Word246       0.0000000
Word247       0.0000000
Word248       0.0000000
Word249       0.0000000
Word250       0.0000000
Word251       0.0000000
Word252       0.0000000
Word253       0.0000000
Word254       0.0000000
Word255       0.0000000
Word256       0.0000000
Word257       0.0000000
Word258       0.6837465
Word259       0.0000000
Word260       0.0000000
Word261       0.0000000
Word262       0.0000000
Word263       0.0000000
Word264       0.0000000
Word265       0.0000000
Word266       0.0000000
Word267       0.0000000
Word268       0.0000000
Word269       0.0000000
Word270       0.0000000
Word271       0.0000000
Word272       0.5574680
Word273       0.0000000
Word274       0.0000000
Word275       0.0000000
Word276       0.0000000
Word277       0.0000000
Word278       0.0000000
Word279       0.0000000
Word280       0.3310252
Word281       0.0000000
Word282       0.0000000
Word283       0.0000000
Word284       0.0000000
Word285       0.0000000
Word286       0.0000000
Word287       0.0000000
Word288       0.0000000
Word289       0.0000000
Word290       0.0000000
Word291       0.3682125
Word292       0.0000000
Word293       0.0000000
Word294       0.0000000
Word295       0.0000000
Word296       0.0000000
Word297       0.0000000
Word298       0.0000000
Word299       0.0000000
Word300       0.0000000
Word301       0.2861000
Word302       0.0000000
Word303       0.0000000
Word304       0.0000000
Word305       0.0000000
Word306       0.0000000
Word307       0.0000000
Word308       0.0000000
Word309       0.0000000
Word310       0.0000000
Word311       0.0000000
Word312       0.0000000
Word313       0.0000000
Word314       0.0000000
Word315       0.0000000
Word316       0.0000000
Word317       0.0000000
Word318       0.0000000
Word319       0.0000000
Word320       0.0000000
Word321       0.0000000
Word322       0.0000000
Word323       0.0000000
Word324       0.0000000
Word325       0.0000000
Word326       0.0000000
Word327       0.0000000
Word328       0.0000000
Word329       0.0000000
Word330       0.0000000
Word331       0.0000000
Word332       0.0000000
Word333       0.3135756
Word334       0.0000000
Word335       0.0000000
Word336       0.0000000
Word337       0.0000000
Word338       0.5574680
Word339       0.0000000
Word340       0.0000000
Word341       0.0000000
Word342       0.0000000
Word343       0.0000000
Word344       0.0000000
Word345       0.7031230
Word346       0.0000000
Word347       0.3798847
Word348       0.0000000
Word349       0.0000000
Word350       0.0000000
Word351       0.0000000
Word352       0.0000000
Word353       0.0000000
Word354       0.0000000
Word355       0.0000000
Word356       0.0000000
Word357       0.0000000
Word358       0.0000000
Word359       0.3347843
Word360       0.0000000
Word361       0.0000000
Word362       0.0000000
Word363       0.0000000
Word364       0.0000000
Word365       0.0000000
Word366       0.0000000
Word367       0.0000000
Word368       0.0000000
Word369       0.0000000
Word370       0.0000000
Word371       0.0000000
Word372       0.0000000
Word373       0.0000000
Word374       0.0000000
Word375       0.0000000
Word376       0.0000000
Word377       0.0000000
Word378       0.0000000
Word379       0.4481581
Word380       0.0000000
Word381       0.0000000
Word382       0.0000000
Word383       0.0000000
Word384       0.0000000
Word385       0.0000000
Word386       0.0000000
Word387       0.8733467
Word388       0.0000000
Word389       0.0000000
Word390       0.0000000
Word391       0.0000000
Word392       0.0000000
Word393       0.0000000
Word394       0.0000000
Word395       0.0000000
Word396       0.0000000
Word397       0.0000000
Word398       0.0000000
Word399       0.0000000
Word400       0.0000000
Word401       0.0000000
Word402       0.0000000
Word403       0.5771650
Word404       0.0000000
Word405       0.0000000
Word406       0.0000000
Word407       0.0000000
Word408       0.0000000
Word409       0.0000000
Word410       0.0000000
Word411       0.0000000
Word412       0.0000000
Word413       0.0000000
Word414       0.0000000
Word415       0.0000000
Word416       0.0000000
Word417       0.0000000
Word418       0.0000000
Word419       0.0000000
Word420       0.0000000
Word421       0.7021981
Word422       0.0000000
Word423       0.0000000
Word424       0.0000000
Word425       0.3971227
Word426       0.0000000
Word427       0.0000000
Word428       0.0000000
Word429       0.0000000
Word430       0.0000000
Word431       0.0000000
Word432       0.0000000
Word433       0.0000000
Word434       0.0000000
Word435       0.0000000
Word436       0.0000000
Word437       0.0000000
Word438       0.0000000
Word439       0.0000000
Word440       0.0000000
Word441       0.0000000
Word442       0.0000000
Word443       0.0000000
Word444       0.0000000
Word445       0.0000000
Word446       0.0000000
Word447       0.4159002
Word448       0.0000000
Word449       0.0000000
Word450       0.0000000
Word451       0.0000000
Word452       0.0000000
Word453       0.0000000
Word454       0.0000000
Word455       0.0000000
Word456       0.3833984
Word457       0.0000000
Word458       0.0000000
Word459       0.0000000
Word460       0.0000000
Word461       0.0000000
Word462       0.0000000
Word463       0.0000000
Word464       0.0000000
Word465       0.0000000
Word466       0.0000000
Word467       0.0000000
Word468       0.0000000
Word469       0.0000000
Word470       0.0000000
Word471       0.0000000
Word472       0.0000000
Word473       0.0000000
Word474       0.0000000
Word475       0.0000000
Word476       0.3432003
Word477       0.0000000
Word478       0.0000000
Word479       0.5199471
Word480       0.0000000
Word481       0.0000000
Word482       0.0000000
Word483       0.0000000
Word484       0.0000000
Word485       0.0000000
Word486       0.0000000
Word487       0.4885618
Word488       0.0000000
Word489       0.0000000
Word490       0.0000000
Word491       0.0000000
Word492       0.0000000
Word493       0.0000000
Word494       0.2925090
Word495       0.0000000
Word496       0.0000000
Word497       0.0000000
Word498       0.0000000
Word499       0.4347532
Word500       0.0000000
Word501       0.0000000
Word502       0.4477269
Word503       0.0000000
Word504       0.0000000
Word505       0.0000000
Word506       0.0000000
Word507       0.0000000
Word508       0.0000000
Word509       0.0000000
Word510       0.0000000
Word511       0.4024218
Word512       0.0000000
Word513       0.4911928
Word514       0.0000000
Word515       0.0000000
Word516       0.0000000
Word517       0.0000000
Word518       0.6103597
Word519       0.7186443
Word520       0.0000000
Word521       0.0000000
Word522       0.0000000
Word523       0.3008058
Word524       0.0000000
Word525       0.0000000
Word526       0.0000000
Word527       0.0000000
Word528       0.0000000
Word529       0.0000000
Word530       0.0000000
Word531       0.0000000
Word532       0.0000000
Word533       0.0000000
Word534       0.0000000
Word535       0.0000000
Word536       0.0000000
Word537       0.0000000
Word538       0.0000000
Word539       0.0000000
Word540       0.0000000
Word541       0.0000000
Word542       0.0000000
Word543       0.0000000
Word544       0.0000000
Word545       0.0000000
Word546       0.9152454
Word547       0.0000000
Word548       0.0000000
Word549       0.0000000
Word550       0.0000000
Word551       0.0000000
Word552       0.0000000
Word553       0.3973915
Word554       0.0000000
Word555       0.0000000
Word556       0.0000000
Word557       0.0000000
Word558       0.0000000
Word559       0.0000000
Word560       0.0000000
Word561       0.5659862
Word562       0.0000000
Word563       0.0000000
Word564       0.0000000
Word565       0.0000000
Word566       0.0000000
Word567       0.0000000
Word568       0.0000000
Word569       0.0000000
Word570       0.0000000
Word571       0.2886572
Word572       0.0000000
Word573       0.0000000
Word574       0.0000000
Word575       0.0000000
Word576       0.0000000
Word577       0.0000000
Word578       0.0000000
Word579       0.0000000
Word580       0.0000000
Word581       0.0000000
Word582       0.0000000
Word583       0.0000000
Word584       0.0000000
Word585       0.0000000
Word586       0.0000000
Word587       0.0000000
Word588       0.0000000
Word589       0.0000000
Word590       0.0000000
Word591       0.0000000
Word592       0.0000000
Word593       0.0000000
Word594       0.0000000
Word595       0.6421223
Word596       0.0000000
Word597       0.0000000
Word598       0.0000000
Word599       0.0000000
Word600       0.0000000
Word601       0.0000000
Word602       0.0000000
Word603       0.0000000
Word604       0.0000000
Word605       0.0000000
Word606       0.0000000
Word607       0.6150991
Word608       0.5150927
Word609       0.0000000
Word610       0.0000000
Word611       0.0000000
Word612       0.0000000
Word613       0.0000000
Word614       0.0000000
Word615       0.4051268
Word616       0.0000000
Word617       0.0000000
Word618       0.0000000
Word619       0.3983752
Word620       0.0000000
Word621       0.0000000
Word622       0.0000000
Word623       0.0000000
Word624       0.0000000
Word625       0.0000000
Word626       0.0000000
Word627       0.0000000
Word628       0.0000000
Word629       0.0000000
Word630       0.0000000
Word631       0.7224903
Word632       0.0000000
Word633       0.0000000
Word634       0.0000000
Word635       0.0000000
Word636       0.0000000
Word637       0.0000000
Word638       0.0000000
Word639       0.0000000
Word640       0.0000000
Word641       0.0000000
Word642       0.0000000
Word643       0.0000000
Word644       0.0000000
Word645       0.0000000
Word646       0.0000000
Word647       0.0000000
Word648       0.0000000
Word649       0.0000000
Word650       0.3211237
Word651       0.0000000
Word652       0.0000000
Word653       0.0000000
Word654       0.0000000
Word655       0.0000000
Word656       0.0000000
Word657       0.0000000
Word658       0.0000000
Word659       0.0000000
Word660       0.0000000
Word661       0.0000000
Word662       0.0000000
Word663       0.0000000
Word664       0.0000000
Word665       0.0000000
Word666       0.0000000
Word667       0.0000000
Word668       0.0000000
Word669       0.0000000
Word670       0.0000000
Word671       0.0000000
Word672       0.0000000
Word673       0.4302838
Word674       0.0000000
Word675       0.0000000
Word676       0.0000000
Word677       0.0000000
Word678       0.0000000
Word679       0.0000000
Word680       0.0000000
Word681       0.0000000
Word682       0.0000000
Word683       0.0000000
Word684       0.3224822
Word685       0.0000000
Word686       0.0000000
Word687       0.3845302
Word688       0.0000000
Word689       0.0000000
Word690       0.0000000
Word691       0.2695573
Word692       0.0000000
Word693       0.0000000
Word694       0.0000000
Word695       0.0000000
Word696       0.0000000
Word697       0.0000000
Word698       0.0000000
Word699       0.0000000
Word700       0.0000000
Word701       0.0000000
Word702       0.0000000
Word703       0.0000000
Word704       0.0000000
Word705       0.7849379
Word706       0.0000000
Word707       0.0000000
Word708       0.0000000
Word709       0.0000000
Word710       0.0000000
Word711       0.0000000
Word712       0.0000000
Word713       0.0000000
Word714       0.3104602
Word715       0.0000000
Word716       0.0000000
Word717       0.0000000
Word718       0.0000000
Word719       0.0000000
Word720       0.0000000
Word721       0.0000000
Word722       0.0000000
Word723       0.0000000
Word724       0.0000000
Word725       0.0000000
Word726       0.4601127
Word727       0.0000000
Word728       0.0000000
Word729       0.0000000
Word730       0.0000000
Word731       0.4679826
Word732       0.0000000
Word733       0.0000000
Word734       0.0000000
Word735       0.0000000
Word736       0.0000000
Word737       0.0000000
Word738       0.0000000
Word739       0.0000000
Word740       0.0000000
Word741       0.0000000
Word742       0.0000000
Word743       0.0000000
Word744       0.0000000
Word745       0.0000000
Word746       0.0000000
Word747       0.0000000
Word748       0.0000000
Word749       0.0000000
Word750       0.0000000
Word751       0.0000000
Word752       0.0000000
Word753       0.0000000
Word754       0.0000000
Word755       0.0000000
Word756       0.0000000
Word757       0.0000000
Word758       0.0000000
Word759       0.0000000
Word760       0.0000000
Word761       0.0000000
Word762       0.0000000
Word763       0.0000000
Word764       0.0000000
Word765       0.0000000
Word766       0.0000000
Word767       0.0000000
Word768       0.0000000
Word769       0.0000000
Word770       0.0000000
Word771       0.0000000
Word772       0.0000000
Word773       0.0000000
Word774       0.0000000
Word775       0.0000000
Word776       0.0000000
Word777       0.0000000
Word778       0.0000000
Word779       0.0000000
Word780       0.0000000
Word781       0.0000000
Word782       0.0000000
Word783       0.0000000
Word784       0.0000000
Word785       0.0000000
Word786       0.0000000
Word787       0.0000000
Word788       0.0000000
Word789       0.0000000
Word790       0.0000000
Word791       0.0000000
Word792       0.0000000
Word793       0.0000000
Word794       0.0000000
Word795       0.0000000
Word796       0.0000000
Word797       0.0000000
Word798       0.0000000
Word799       0.2837405
Word800       0.0000000
Word801       0.0000000
Word802       0.0000000
Word803       0.0000000
Word804       0.0000000
Word805       0.0000000
Word806       0.0000000
Word807       0.0000000
Word808       0.0000000
Word809       0.0000000
Word810       0.0000000
Word811       0.0000000
Word812       0.0000000
Word813       0.0000000
Word814       0.5742260
Word815       0.0000000
Word816       0.0000000
Word817       0.0000000
Word818       0.0000000
Word819       0.0000000
Word820       0.0000000
Word821       0.0000000
Word822       0.0000000
Word823       0.3922502
Word824       0.0000000
Word825       0.0000000
Word826       0.0000000
Word827       0.0000000
Word828       0.0000000
Word829       0.0000000
Word830       0.0000000
Word831       0.0000000
Word832       0.5772561
Word833       0.0000000
Word834       0.0000000
Word835       0.0000000
Word836       0.0000000
Word837       0.2886572
Word838       0.0000000
Word839       0.3063720
Word840       0.0000000
Word841       0.0000000
Word842       0.0000000
Word843       0.0000000
Word844       0.0000000
Word845       0.0000000
Word846       0.0000000
Word847       0.0000000
Word848       0.0000000
Word849       0.0000000
Word850       0.0000000
Word851       0.7133812
Word852       0.0000000
Word853       0.0000000
Word854       0.0000000
Word855       0.3967638
Word856       0.0000000

由于数据集braz.csv由856个属性组成。在它上面应用卡方,我们得到每个属性各自的值。我想在此函数中传递一个变量,该变量将选择值大于0的属性。

1 个答案:

答案 0 :(得分:0)

这将使所有值大于零:

df <- read.csv('tmptabsep.txt', sep = ',', stringsAsFactors = F)

df[df[,2] > 0,]

注意:我将您的文档更改为逗号分隔而不是所有这些空格(这些空格似乎不是标签,至少,解析器并不理解它)。

示例数据:

Word1,0.0000000
Word2,0.0000000
Word3,0.0000000
Word4,0.0000000
Word5,0.0000000
Word6,0.0000000
Word7,0.4224098
Word8,0.0000000
Word9,0.0000000
Word10,0.0000000
Word11,0.3544805
Word12,0.2957039
Word13,0.0000000
Word14,0.0000000
Word15,0.0000000
Word16,0.0000000
Word17,0.0000000
Word18,0.0000000
Word19,0.0000000
Word20,0.0000000
Word21,0.0000000
Word22,0.0000000
Word23,0.0000000
Word24,0.0000000
Word25,0.0000000
Word26,0.0000000
Word27,0.0000000
Word28,0.0000000
Word29,0.0000000

示例输出:

> df[df[,2] > 0,]
    Word1 X0.0000000
6   Word7  0.4224098
10 Word11  0.3544805
11 Word12  0.2957039
> df[df[,2] > 0,1] # get words only
[1] "Word7"  "Word11" "Word12"

修改:根据您的评论,您的方法会返回list。所以,你应该能够做到这一点:

f<-function(data) {
    braz<-read.csv("braz.csv",header=T)
    index<-1:nrow(braz)
    tindex<-sample(index,trunc(length(index)*.7))
    trainbraz<-braz[tindex,]
    testbraz<-braz[-tindex,]
    weights<-chi.squared(Class~.,trainbraz)
    weightsGreaterThanZero <- weights[weights > 0]
    return(weightsGreaterThanZero)
}