[R] Basic structure operations doubt

Joshua Wiley jwiley.psych at gmail.com
Mon Oct 18 04:16:31 CEST 2010


Hi,

The easiest way to get rid of the empty levels is with droplevels().
See ?droplevels for details.  It actually has a method for data frames
even.  So you could just do something like:

Indx_Constituents <- droplevels(Indx_Constituents)

or whatever your data frame was called and it will drop any unused
levels for you.

Cheers,

Josh

On Sun, Oct 17, 2010 at 7:06 PM, Santosh Srinivas
<santosh.srinivas at gmail.com> wrote:
> I'm doing these manipulations on the data frame and wondering why does R
> have to remember historical data on my operation and not just keep the
> needed info.
> Probably a basic fundamentals of the way R handles data .. Pls point me to
> the manual if possible ..
>
> I have this Index data:
>> head(NIFTY_INDX)
>  Constituents.list.of.S.P.CNX.Nifty                          X       X.1
> X.2          X.3
> 1
>
> 2                       Company Name                   Industry    Symbol
> Series    ISIN Code
> 3
>
> 4                           ACC Ltd. CEMENT AND CEMENT PRODUCTS       ACC
> EQ INE012A01025
> 5                Ambuja Cements Ltd. CEMENT AND CEMENT PRODUCTS AMBUJACEM
> EQ INE079A01024
> 6                     Axis Bank Ltd.                      BANKS  AXISBANK
> EQ INE238A01026
>
>
> I Import the section that is relevant to me:
>
>> Indx_Constituents <- NIFTY_INDX[4:NROW(NIFTY_INDX),]
>> head(Indx_Constituents)
>  Constituents.list.of.S.P.CNX.Nifty                              X
> X.1 X.2          X.3
> 4                           ACC Ltd.     CEMENT AND CEMENT PRODUCTS
> ACC  EQ INE012A01025
> 5                Ambuja Cements Ltd.     CEMENT AND CEMENT PRODUCTS
> AMBUJACEM  EQ INE079A01024
> 6                     Axis Bank Ltd.                          BANKS
> AXISBANK  EQ INE238A01026
> 7                    Bajaj Auto Ltd. AUTOMOBILES - 2 AND 3 WHEELERS
> BAJAJ-AUTO  EQ INE917I01010
> 8      Bharat Heavy Electricals Ltd.           ELECTRICAL EQUIPMENT
> BHEL  EQ INE257A01018
> 9  Bharat Petroleum Corporation Ltd.                     REFINERIES
> BPCL  EQ INE029A01011
>
>
>> colNames <- NIFTY_INDX[2,]
>> colNames
>  Constituents.list.of.S.P.CNX.Nifty        X    X.1    X.2       X.3
> 2                       Company Name Industry Symbol Series ISIN Code
>
>
> I want to assign the info from colNames[1,] to Indx_Constituents .... I am
> unable to do this directly ... I can probably pull out the values and do it
> but there should be an easier way
>
>
> Now when I do this:
>> colNames[1,1]
> [1] Company Name
> 52 Levels:  ACC Ltd. Ambuja Cements Ltd. Axis Bank Ltd. Bajaj Auto Ltd.
> Bharat Heavy Electricals Ltd. Bharat Petroleum Corporation Ltd. Bharti
> Airtel Ltd. Cairn India Ltd. Cipla Ltd. Company Name ... Wipro Ltd.
>
> Why does R have to remember the 52 levels?? Why can't it just have the
> relevant data stored
> What are the alternatives so that I can simply have my needed data in my
> data frames?
>
> Thanks for your explanation.
>
>
> ----------------------------------------------------------------------------
> --------------------------
> Thanks R-Helpers. Yes, this is a silly question and it will not be repeated!
> :-)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list