Sorry, I'm sure I'm not using the appropriate vocab here, which is undoubtedly why I can't seem to find a fix to this (hopefully very easy) problem. Suppose you have a factor abc <- factor(c(2,2,3,4,7,7)) And you want to know what the number in the nth spot in that would be abc[1] [1] 2 Levels: 2 3 4 7 shows the correct label of the first element - but if I want to pull out the numeric value of that label, I thought... as.numeric(abc[1]) but that gives [1] 1 which is the position of the label in the levels vector of the factor. Ideas? Thanks!
Numeric "Label" of Factor value?
4 messages · Brigid Mooney, Rui Barradas, Sarah Goslee
Hi Brigid, as.numeric() extracts the index of the factor level, which is the way R handles the likelihood that a factor is not actually numeric. Try:
as.numeric(as.character(abc[1]))
[1] 2 and see also ?factor particularly the section on the interpretation of a factor. Sarah
On Wed, Oct 10, 2012 at 2:39 PM, Brigid Mooney <bkmooney at gmail.com> wrote:
Sorry, I'm sure I'm not using the appropriate vocab here, which is undoubtedly why I can't seem to find a fix to this (hopefully very easy) problem. Suppose you have a factor abc <- factor(c(2,2,3,4,7,7)) And you want to know what the number in the nth spot in that would be abc[1] [1] 2 Levels: 2 3 4 7 shows the correct label of the first element - but if I want to pull out the numeric value of that label, I thought... as.numeric(abc[1]) but that gives [1] 1 which is the position of the label in the levels vector of the factor. Ideas? Thanks!
______________________________________________ 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.
Hello, Try instead ?levels abc <- factor(c(2,2,3,4,7,7)) as.numeric(levels(abc)[1]) Hope this helps, Rui Barradas Em 10-10-2012 19:39, Brigid Mooney escreveu:
Sorry, I'm sure I'm not using the appropriate vocab here, which is undoubtedly why I can't seem to find a fix to this (hopefully very easy) problem. Suppose you have a factor abc <- factor(c(2,2,3,4,7,7)) And you want to know what the number in the nth spot in that would be abc[1] [1] 2 Levels: 2 3 4 7 shows the correct label of the first element - but if I want to pull out the numeric value of that label, I thought... as.numeric(abc[1]) but that gives [1] 1 which is the position of the label in the levels vector of the factor. Ideas? Thanks!
______________________________________________ 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.
Rui, that doesn't answer the question as I understood it: Your suggestion returns the numeric value of the second value of the levels:
as.numeric(levels(abc)[2])
[1] 3 But I read the question as wanting the numeric value of the second element of abc:
as.numeric(as.character(abc[2]))
[1] 2
On Wed, Oct 10, 2012 at 2:52 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
Hello, Try instead ?levels abc <- factor(c(2,2,3,4,7,7)) as.numeric(levels(abc)[1]) Hope this helps, Rui Barradas Em 10-10-2012 19:39, Brigid Mooney escreveu:
Sorry, I'm sure I'm not using the appropriate vocab here, which is undoubtedly why I can't seem to find a fix to this (hopefully very easy) problem. Suppose you have a factor abc <- factor(c(2,2,3,4,7,7)) And you want to know what the number in the nth spot in that would be abc[1] [1] 2 Levels: 2 3 4 7 shows the correct label of the first element - but if I want to pull out the numeric value of that label, I thought... as.numeric(abc[1]) but that gives [1] 1 which is the position of the label in the levels vector of the factor. Ideas? Thanks!
Sarah Goslee http://www.functionaldiversity.org