Can't Destroy Dim Names

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't Destroy Dim Names

Gundala Viswanath
Dear all,

I have the following matrix:

> str(mat)
Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...
 - attr(*, "names")= chr [1:32268] "CAAAAAAAAA" "CAAAAAAAAC"
"CAAAAAAAAG" "CAAAAAAAAT" ...


I want to destroy the attribute yielding only this:

> str(mat)
Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...


But why this command fail to do it?

> dimnames(mat) <- NULL

Btw, the object size for keeping matrix with dim names is greater
than without, right?



- Gundala Viswanath
Jakarta - Indonesia

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Can't Destroy Dim Names

jholtman
Is this what you want:

> x <- c(a=1, b=2, c=3)
> str(x)
 Named num [1:3] 1 2 3
 - attr(*, "names")= chr [1:3] "a" "b" "c"
> names(x) <- NULL
> x
[1] 1 2 3
>

On Mon, Jan 12, 2009 at 8:32 PM, Gundala Viswanath <[hidden email]> wrote:

> Dear all,
>
> I have the following matrix:
>
>> str(mat)
> Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...
>  - attr(*, "names")= chr [1:32268] "CAAAAAAAAA" "CAAAAAAAAC"
> "CAAAAAAAAG" "CAAAAAAAAT" ...
>
>
> I want to destroy the attribute yielding only this:
>
>> str(mat)
> Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...
>
>
> But why this command fail to do it?
>
>> dimnames(mat) <- NULL
>
> Btw, the object size for keeping matrix with dim names is greater
> than without, right?
>
>
>
> - Gundala Viswanath
> Jakarta - Indonesia
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Can't Destroy Dim Names

Greg Snow-2
In reply to this post by Gundala Viswanath
Your mat variable is not a matrix, but a vector (a named vector), therefore it does not have dimensions or dimnames.

Try names(mat) <- NULL

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Gundala Viswanath
> Sent: Monday, January 12, 2009 6:32 PM
> To: [hidden email]
> Subject: [R] Can't Destroy Dim Names
>
> Dear all,
>
> I have the following matrix:
>
> > str(mat)
> Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...
>  - attr(*, "names")= chr [1:32268] "CAAAAAAAAA" "CAAAAAAAAC"
> "CAAAAAAAAG" "CAAAAAAAAT" ...
>
>
> I want to destroy the attribute yielding only this:
>
> > str(mat)
> Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...
>
>
> But why this command fail to do it?
>
> > dimnames(mat) <- NULL
>
> Btw, the object size for keeping matrix with dim names is greater
> than without, right?
>
>
>
> - Gundala Viswanath
> Jakarta - Indonesia
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Can't Destroy Dim Names

clangkamp
In reply to this post by jholtman
Dear Jim
I think the target is to get from a Named chr to a just chr
>> str(mat)
> Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...
>  - attr(*, "names")= chr [1:32268] "CAAAAAAAAA" "CAAAAAAAAC"
> "CAAAAAAAAG" "CAAAAAAAAT" ...

I have presumably the same problem
> str(DC1a)
 num [1:18, 1:48, 1:35] 3124.4 3049.2 227.8 41.4 76 ...
 - attr(*, "dimnames")=List of 3
  ..$ Figure  : Named chr [1:18]  "CDS1" ...
  .. ..- attr(*, "names")= chr [1:18] "1"
  ..$ Code    : Named chr [1:48] "AGR"  
.. ..- attr(*, "names")= chr [1:48] "1" "36" "71" "106" ...
  ..$ variable: Named chr [1:35] "X30.09.2009" "  
.. ..- attr(*, "names")= chr [1:35] "1" "2" "3" "4" ...
> DC1_SM<-abind(DC1a, DC1_PLCF_SM1, along=1, new.names=)
> str(DC1_SM)
 num [1:24, 1:48, 1:35] 3124.4 3049.2 227.8 41.4 76 ...
 - attr(*, "dimnames")=List of 3
  ..$ : chr [1:24]  "CDS1" ...
  ..$ : chr [1:48] "AGR"  
  ..$ : chr [1:35] "X30.09.2009" "

names(dimnames(DC1_PLCF_SM1))<-names(dimnames(DC1a))

The point is to kill the lines with the bit
.. ..- attr(*, "names")= chr [1:35] "1" "2" "3" "4" ...
and change the "Named chr" into a plain chr.
Christian Langkamp
christian.langkamp-at-gmxpro.de
Reply | Threaded
Open this post in threaded view
|

Re: Can't Destroy Dim Names

Phil Spector
Does this help?

> a = c(one=1,two=2,three=3)
> str(a)
  Named num [1:3] 1 2 3
  - attr(*, "names")= chr [1:3] "one" "two" "three"
> names(a) = NULL
> str(a)
  num [1:3] 1 2 3

For a matrix called mat, you'd need to use

dimnames(mat) = NULL

to remove the dimnames.

But remember that, other than when you're displaying the
object, the names or dimnames should not affect anything
you do with the object, and they do allow you to index
elements by name instead of number.

  - Phil Spector
  Statistical Computing Facility
  Department of Statistics
  UC Berkeley
  [hidden email]


On Tue, 30 Nov 2010, clangkamp wrote:

>
> Dear Jim
> I think the target is to get from a Named chr to a just chr
>>> str(mat)
>> Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...
>>  - attr(*, "names")= chr [1:32268] "CAAAAAAAAA" "CAAAAAAAAC"
>> "CAAAAAAAAG" "CAAAAAAAAT" ...
>
> I have presumably the same problem
>> str(DC1a)
> num [1:18, 1:48, 1:35] 3124.4 3049.2 227.8 41.4 76 ...
> - attr(*, "dimnames")=List of 3
>  ..$ Figure  : Named chr [1:18]  "CDS1" ...
>  .. ..- attr(*, "names")= chr [1:18] "1"
>  ..$ Code    : Named chr [1:48] "AGR"
> .. ..- attr(*, "names")= chr [1:48] "1" "36" "71" "106" ...
>  ..$ variable: Named chr [1:35] "X30.09.2009" "
> .. ..- attr(*, "names")= chr [1:35] "1" "2" "3" "4" ...
>> DC1_SM<-abind(DC1a, DC1_PLCF_SM1, along=1, new.names=)
>> str(DC1_SM)
> num [1:24, 1:48, 1:35] 3124.4 3049.2 227.8 41.4 76 ...
> - attr(*, "dimnames")=List of 3
>  ..$ : chr [1:24]  "CDS1" ...
>  ..$ : chr [1:48] "AGR"
>  ..$ : chr [1:35] "X30.09.2009" "
>
> names(dimnames(DC1_PLCF_SM1))<-names(dimnames(DC1a))
>
> The point is to kill the lines with the bit
> .. ..- attr(*, "names")= chr [1:35] "1" "2" "3" "4" ...
> and change the "Named chr" into a plain chr.
>
> -----
> Christian Langkamp
> christian.langkamp-at-gmxpro.de
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Can-t-Destroy-Dim-Names-tp876633p3066413.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Can't Destroy Dim Names

David Winsemius
In reply to this post by clangkamp

On Nov 30, 2010, at 6:07 PM, clangkamp wrote:

> Dear Jim
> I think the target is to get from a Named chr to a just chr
>>> str(mat)
>> Named chr [1:32268] "yQAAA" "jQAAQ" "UQAAg" "FQAAw" "1QABA" ...
>> - attr(*, "names")= chr [1:32268] "CAAAAAAAAA" "CAAAAAAAAC"
>> "CAAAAAAAAG" "CAAAAAAAAT" ...
>
> I have presumably the same problem
>> str(DC1a)
> num [1:18, 1:48, 1:35] 3124.4 3049.2 227.8 41.4 76 ...
> - attr(*, "dimnames")=List of 3
>  ..$ Figure  : Named chr [1:18]  "CDS1" ...
>  .. ..- attr(*, "names")= chr [1:18] "1"
>  ..$ Code    : Named chr [1:48] "AGR"
> .. ..- attr(*, "names")= chr [1:48] "1" "36" "71" "106" ...
>  ..$ variable: Named chr [1:35] "X30.09.2009" "
> .. ..- attr(*, "names")= chr [1:35] "1" "2" "3" "4" ...
>> DC1_SM<-abind(DC1a, DC1_PLCF_SM1, along=1, new.names=)
>> str(DC1_SM)
> num [1:24, 1:48, 1:35] 3124.4 3049.2 227.8 41.4 76 ...
> - attr(*, "dimnames")=List of 3
>  ..$ : chr [1:24]  "CDS1" ...
>  ..$ : chr [1:48] "AGR"
>  ..$ : chr [1:35] "X30.09.2009" "
>
> names(dimnames(DC1_PLCF_SM1))<-names(dimnames(DC1a))
>
> The point is to kill the lines with the bit
> .. ..- attr(*, "names")= chr [1:35] "1" "2" "3" "4" ...
> and change the "Named chr" into a plain chr.

It is not at all clear to me that the problem posed a year and a half  
ago is the same as the one you perceive you are facing. In any event  
you are welcome to mangle your object (which you have not offered for  
testing) by turning a named dimension name vector into an unnamed one:

?unname
?Extract

DCtest <- array(1:27, c(3,3,3))
dimnames(DCtest) <- list(dim1 =c(a="a",b="b",c="c"),   #named vector
                             dim2=letters[4:6],            #unnamed  
vectors
                             dim3= letters[7:9])

 > str(DCtest)
  int [1:3, 1:3, 1:3] 1 2 3 4 5 6 7 8 9 10 ...
  - attr(*, "dimnames")=List of 3
   ..$ dim1: Named chr [1:3] "a" "b" "c"
   .. ..- attr(*, "names")= chr [1:3] "a" "b" "c"
   ..$ dim2: chr [1:3] "d" "e" "f"
   ..$ dim3: chr [1:3] "g" "h" "i"
 > dimnames(DCtest)[1]
$dim1
   a   b   c
"a" "b" "c"

 > dimnames(DCtest)[[1]]
   a   b   c
"a" "b" "c"

So use the [[<- function to replace the named vector with an unnamed  
one:

 > dimnames(DCtest)[[1]] <- unname( dimnames(DCtest)[[1]] )
 > str(DCtest)
  int [1:3, 1:3, 1:3] 1 2 3 4 5 6 7 8 9 10 ...
  - attr(*, "dimnames")=List of 3
   ..$ dim1: chr [1:3] "a" "b" "c"
   ..$ dim2: chr [1:3] "d" "e" "f"
   ..$ dim3: chr [1:3] "g" "h" "i"

>
> -----
> Christian Langkamp
> christian.langkamp-at-gmxpro.de
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Can-t-Destroy-Dim-Names-tp876633p3066413.html

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Can't Destroy Dim Names

clangkamp
Dear David
To confirm, your method worked. I am not allowed to divulge the original data (financial data), but here comes another set.

Test_DF<-data.frame(Figure=c("A","B","A","B"), Code=c("C", "C", "D", "D"), No=c(1,2,3.5,4))
?data.frame
View(Test_DF)
str(Test_DF)
Test_DF_m<-melt(Test_DF, measure="No")
View(Test_DF_m)
Test_DF_c<-cast(Test_DF_m, Figure~Code)
str(Test_DF_c)

Basically, after the casting the variables the naming appears. If one then goes forth with this item, and does some operations on them (in my case with the zoo package), and ends up with the same cube, but just chr as row/column names but actually the same ones and then tries to merge them (abind). The set above doesn't replicate strangely everything my set does, ind adds a lot more attributes even though I go through the same commands.

So the bottom line is where I would expect a simple 2 by 2 matrix with row and column names after the melt and cast, it is a much more complex object and I think tearing off all the rest might be a good thing before proceeding. This is the background to the question.
Christian Langkamp
christian.langkamp-at-gmxpro.de