0

NB: Quite a beginner, so probably missing something obvious.

I am trying to write some generalizable code to do some factor-based analysis in R. I have a list of factors and their levels such that

$group
[1] "NR" "NV" "NT"

$device
[1] "X" "Y" 

$width
[1] "fat" "nom"

$length
[1] "long" "nom" 

I'm trying to write a loop to extract the individual levels of each factor in sequence (in the first case, "NR"), without using the name of the factor since that will change with different data sets.

I can get all the levels of a given factor by

factors[1]

or an individual level by

factors$group[1]

but how do I extract an individual level in a way that doesn't use the name of the factor? It can be done in a couple of steps, by for example

demo <- factors[[1]]
demo[1]

but that seems sloppy, since I don't actually need the variable demo to exist. Is there a neater way?

4

1 回答 1

0

当然,在我提出问题后立即想通了。如果其他人需要这个, [[ 和 [ 可以同时使用,所以

factors[[1]][1]

"NR"
于 2013-06-11T14:50:35.480 回答