[R] Problem in 'Apply' function: does anybody have other solution

jude.ryan at ubs.com jude.ryan at ubs.com
Thu Jun 18 16:59:28 CEST 2009


David Winsemius' solution:

 

> apply(data.matrix(df), 1, I)

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

x    1    2    3    4    5    6    7    8    9    10

y    1    3    4    5    6    7    8    9   10     2

 

For y and [,2] above the value is 3. Why is the value not 2? 

It looks like the value is 2 for y and [,10] (this should be 10, right?)

and values 3 to 10 are shifted one position to the left for "y".

 

I got the same results when I ran this code.

 

Thanks,

 

Jude

 

David Winsemius wrote:

 

On Jun 17, 2009, at 9:27 AM, jim holtman wrote:

 

> Do an 'str' of your object.  It looks like one of the columns is  

> probably

> character/factor since there are quotes around the 'numbers'.  You  

> can also

> explicity convert the offending columns to numeric is you want to.   

> Also use

> colClasses on the read.csv to define the class of the data in each  

> column.

> This will should you where the error is.

 

One function that might be of use is data.matrix which will attempt to  

convert character vectors to numeric vectors across an entire  

dataframe. I hope this is not beating a dead horse, but see if these  

examples are helpful in any way:

 

 > ?data.matrix

 > df <- data.frame(x=1:10,y=as.character(1:10))

 > df

     x  y

1   1  1

2   2  2

3   3  3

4   4  4

5   5  5

6   6  6

7   7  7

8   8  8

9   9  9

10 10 10    # .... not all is as it seems

 > apply(df,1,I)

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

x " 1" " 2" " 3" " 4" " 5" " 6" " 7" " 8" " 9" "10"

y "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

 > df2 <- data.frame(x=1:10,y=1:10)

 > apply(df2,1,I)

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

x    1    2    3    4    5    6    7    8    9    10

y    1    2    3    4    5    6    7    8    9    10

 > str(df)

'data.frame':     10 obs. of  2 variables:

  $ x: int  1 2 3 4 5 6 7 8 9 10

  $ y: Factor w/ 10 levels "1","10","2","3",..: 1 3 4 5 6 7 8 9 10 2

 

# so that's weird. y isn't even a character vector !?!? Such are the  

strange beasts called factors.

 

# solution? or at least one strategy

 

 > apply(data.matrix(df), 1, I)

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

x    1    2    3    4    5    6    7    8    9    10

y    1    3    4    5    6    7    8    9   10     2

 

 

 

 

___________________________________________
Jude Ryan
Director, Client Analytical Services
Strategy & Business Development
UBS Financial Services Inc.
1200 Harbor Boulevard, 4th Floor
Weehawken, NJ 07086-6791
Tel. 201-352-1935
Fax 201-272-2914
Email: jude.ryan at ubs.com



-------------- next part --------------
Please do not transmit orders or instructions regarding a UBS 
account electronically, including but not limited to e-mail, 
fax, text or instant messaging. The information provided in 
this e-mail or any attachments is not an official transaction 
confirmation or account statement. For your protection, do not 
include account numbers, Social Security numbers, credit card 
numbers, passwords or other non-public information in your e-mail. 
Because the information contained in this message may be privileged, 
confidential, proprietary or otherwise protected from disclosure, 
please notify us immediately by replying to this message and 
deleting it from your computer if you have received this 
communication in error. Thank you. 

UBS Financial Services Inc. 
UBS International Inc. 
UBS Financial Services Incorporated of Puerto Rico 
UBS AG

 
UBS reserves the right to retain all messages. Messages are protected
and accessed only in legally justified cases.


More information about the R-help mailing list