dB'*_*dB' 5 r categorical-data
我有一个因素instrumentF:
> instrumentF
[1] Guitar Drums Cello Harp
Levels: Cello Drums Guitar Harp
Run Code Online (Sandbox Code Playgroud)
假设我使用[].
> level2 = instrumentF[1]
> level2
[1] Guitar
Levels: Cello Drums Guitar Harp
Run Code Online (Sandbox Code Playgroud)
如何Guitar从因子对象中获取因子标签level2?
level2$level 似乎不起作用:
> Error in level2$level : $ operator is invalid for atomic vectors
Run Code Online (Sandbox Code Playgroud)
转换为字符,看这个例子:
# factor variable example
instrumentF <- as.factor(c("Guitar", "Drums", "Cello", "Harp"))
instrumentF
# [1] Guitar Drums Cello Harp
# Levels: Cello Drums Guitar Harp
as.character(instrumentF)[ 1 ]
# [1] "Guitar"
Run Code Online (Sandbox Code Playgroud)
请参阅相关帖子:将 data.frame 列从因子转换为字符
或子集级别:
# as levels are sorted alphabetically, we need to subset the 3rd one
levels(instrumentF)[ 3 ]
# [1] "Guitar"
Run Code Online (Sandbox Code Playgroud)