Hello, I am using names function to name an array. It works first time when I use *as.numeric(names(myVar1)* However, at a place later, when I tried to use a very similar line of code *as.numeric(names(myVar2)*, it always returned 'numeric(0)' (or if I only type 'names(myVar2), it gave me NULL'. Both myVar1 and myVar2 are type of integer. The only difference that I can tell is where the names function are called in my code. Does anyone have an idea what may cause the problem? Thanks in advance. -- View this message in context: http://r.789695.n4.nabble.com/names-function-not-working-at-the-2nd-place-in-a-program-tp4638004.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2012-Jul-26 22:06 UTC
[R] names function not working at the 2nd place in a program
Hello, About the cause of your problem, it's difficult to have an idea without any code. But maybe it's helpfull to know that 'names' is an attribute, so you can see its value with attr(x, "names") # just 'names' attribute attributes(x) # values of all attributes Hope this helps, Rui Barradas Em 26-07-2012 22:19, zz escreveu:> Hello, > > I am using names function to name an array. > It works first time when I use *as.numeric(names(myVar1)* > However, at a place later, when I tried to use a very similar line of code > *as.numeric(names(myVar2)*, it always returned 'numeric(0)' (or if I only > type 'names(myVar2), it gave me NULL'. > > Both myVar1 and myVar2 are type of integer. The only difference that I can > tell is where the names function are called in my code. > > Does anyone have an idea what may cause the problem? > > Thanks in advance. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/names-function-not-working-at-the-2nd-place-in-a-program-tp4638004.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
HI, I am not getting errors with this: ?myvar1<- array(1:3, c(2,4)) ?myvar2<- array(4:6, c(2,4)) ?names(myvar1) #NULL names(myvar1)<-as.numeric(1:8) names(myvar1) [1] "1" "2" "3" "4" "5" "6" "7" "8" ?names(myvar2)<-names(myvar1) names(myvar2) [1] "1" "2" "3" "4" "5" "6" "7" "8" #But, if you look at str() ?str(myvar2) ?int [1:2, 1:4] 4 5 6 4 5 6 4 5 ?- attr(*, "names")= chr [1:8] "1" "2" "3" "4" ... ?is.numeric(names(myvar2)) [1] FALSE A.K. ----- Original Message ----- From: zz <czhang at uams.edu> To: r-help at r-project.org Cc: Sent: Thursday, July 26, 2012 5:19 PM Subject: [R] names function not working at the 2nd place in a program Hello, I am using names function to name an array. It works first time when I use *as.numeric(names(myVar1)* However, at a place later, when I tried to use a very similar line of code *as.numeric(names(myVar2)*, it always returned 'numeric(0)' (or if I only type 'names(myVar2), it gave me NULL'. Both myVar1 and myVar2 are type of integer.? The only difference that I can tell is where the names function are called in my code. Does anyone have an idea what may cause the problem? Thanks in advance. -- View this message in context: http://r.789695.n4.nabble.com/names-function-not-working-at-the-2nd-place-in-a-program-tp4638004.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
MacQueen, Don
2012-Jul-26 22:20 UTC
[R] names function not working at the 2nd place in a program
I rather suspect you don't understand what it means to name an array. Consider this example:> myvar <- 1:4> myvar[1] 1 2 3 4> names(myvar)NULL> names(myvar) <- letters[1:4] > myvara b c d 1 2 3 4> names(myvar)[1] "a" "b" "c" "d"> as.numeric(names(myvar))[1] NA NA NA NA Warning message: NAs introduced by coercion For sure, as.numeric(names(myVar1)) does not name the array. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 7/26/12 2:19 PM, "zz" <czhang at uams.edu> wrote:>Hello, > >I am using names function to name an array. >It works first time when I use *as.numeric(names(myVar1)* >However, at a place later, when I tried to use a very similar line of code >*as.numeric(names(myVar2)*, it always returned 'numeric(0)' (or if I only >type 'names(myVar2), it gave me NULL'. > >Both myVar1 and myVar2 are type of integer. The only difference that I >can >tell is where the names function are called in my code. > >Does anyone have an idea what may cause the problem? > >Thanks in advance. > > > >-- >View this message in context: >http://r.789695.n4.nabble.com/names-function-not-working-at-the-2nd-place- >in-a-program-tp4638004.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
I just realize this: For myVar1, I used colnames to name it firstly, and then convert myVar1 to numeric; However, I forgot to use rownames to name myVar2, instead I thought as.numeric(names(myVar2)) would work. Now I name myVar2 using rownames first, as.numeric(names(myVar2)) is working. Thank you for pointing the mistake. -- View this message in context: http://r.789695.n4.nabble.com/names-function-not-working-at-the-2nd-place-in-a-program-tp4638004p4638122.html Sent from the R help mailing list archive at Nabble.com.