R数据帧因子水平

时间:2015-01-18 12:09:57

标签: r r-factor

我有这段代码:

levels(testing[,c('is_top_rated_listing')])

返回:

NULL

但是这段代码会返回:

levels(testing$is_top_rated_listing)

返回

"0" "1"

不幸的是,我需要使用第一种方法,因为列名会动态更改。 我知道如何利用第一种方法?

输出输出:

 structure(list(seller_feedback_score = c(321350, 138351, 282470, 
1596, 275283, 275283), is_top_rated_listing = structure(c(2L, 
1L, 2L, 1L, 2L, 1L), .Label = c("0", "1"), class = "factor"), 
    seller_is_top_rated_seller = c(1L, 0L, 1L, 0L, 1L, 1L), is_auto_pay = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"), 
    is_returns_accepted = structure(c(2L, 1L, 2L, 2L, 2L, 2L), .Label = c("0", 
    "1"), class = "factor"), seller_feedback_rating_star = c("RedShooting", 
    "RedShooting", "RedShooting", "Red", "RedShooting", "RedShooting"
    ), keywords_title_assoc = c(1, 0.666666666666667, 1, 0.333333333333333, 
    1, 1), normalized.price_shipping = c(0.0780598804064508, 
    0.0883814448796398, 0.0780598804064508, 0.0777079296934893, 
    0.0254380218279135, 0.0777079296934893), seller_feedback_score_rank = c(9504L, 
    28866L, 19445L, 50280L, 21796L, 21796L), seller_positive_feedback_percent_rank = c(15L, 
    9L, 10L, 4L, 5L, 5L), item_condition = structure(c(1L, 1L, 
    1L, 1L, 1L, 1L), .Label = c("New", "New other (see details)"
    ), class = "factor"), rank = c(9L, 11L, 13L, 21L, 3L, 4L)), .Names = c("seller_feedback_score", 
"is_top_rated_listing", "seller_is_top_rated_seller", "is_auto_pay", 
"is_returns_accepted", "seller_feedback_rating_star", "keywords_title_assoc", 
"normalized.price_shipping", "seller_feedback_score_rank", "seller_positive_feedback_percent_rank", 
"item_condition", "rank"), class = c("tbl_df", "data.frame"), row.names = c(NA, 
-6L))

1 个答案:

答案 0 :(得分:0)

以下是答案:

levels(testing[,col][[1]])

我很高兴我回答了我的第一个筹码问: - )