[R] Problem assigning "NA" as a level name in a list
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Tue Dec 16 17:57:06 CET 2008
Cliff Behrens wrote:
> Peter,
>
> OK...here is reproducible, self-contained code:
>
> library(gregmisc)
Relying on a 3rd party package is not kosher either... Whatever did
list("NA"=2) or l <- list(2); names(l) <- "NA" do to you?
> columnNames <- c("A","B","C","D","N","a","b","c")
> namePerms<- permutations(length(columnNames),2,columnNames,repeats=TRUE)
> nameList <- paste(namePerms[,1],namePerms[,2],sep="")
> dataList <- lapply(1:length(nameList), function(level) {})
> names(dataList)<- nameList ## The "NA" is interpreted that the name is
> missing for one list in dataList
>
> If you inspect the contents of dataList, you will find the following
> showing that the name "NA" is treated differently:
Anyways.... As I thought:
Remember that NA is a reserved word. You get the same kind of reaction
if you name an element "for" or "in". It denotes that you need to quote
the name for indexing with $:
> names(l) <- "NA"
> l$NA
Error: unexpected numeric constant in "l$NA"
> l$`NA`
[1] 2
> l$"NA"
[1] 2
> l[["NA"]]
[1] 2
> names(l)
[1] "NA"
> ......
>
> $Na
> NULL
>
> $`NA`
> NULL
>
> $Nb
> NULL
> .........
> Peter Dalgaard wrote:
>> Cliff Behrens wrote:
>>> I want to generate a list (called "dataList" below) where each of its
>>> levels is named. These names are assigned to nameList, which
>>> contains all possible permutations of size two taking letters from a
>>> larger alphabet, e.g., "aa",...,"Fd",..,"Z1",... One of these
>>> permutations is the character string "NA". It seems that when I try
>>> to name one of the dataList levels "NA", using names(dataList)<-
>>> nameList, the names() function assigns the missing character to the
>>> level. Is there someway to preserve "NA" as the name of a level in
>>> dataList? Here is the R code I have been using to do this.
>>>
>>> namePerms<- permutations(ncol(coinMat),2,colnames(coinMat),repeats=TRUE)
>>> nameList <- paste(namePerms[,1],namePerms[,2],sep="")
>>> dataList <- lapply(1:length(nameList), function(level) {})
>>> names(dataList)<- nameList ## The "NA" in nameList is
>>> interpreted so that the name "NA" is missing for one level in dataList
>>>
>>> I am running R 2.4.1 in the Windows XP environment.
>>>
>>> Thanks for any help that can be offerred.
>>
>> Your example is not reproducible and self-contained. What is
>> "permutations" and "coinMat"??
>>
>> I bet it isn't minimal either.
>>
>> It doesn't seem to be happening for me with a recent(!) version of R,
>> but you could just be misinterpreting the backtick quoting.
>>
>> -
>> O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
>> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
>> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
>> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
>
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list