提取R data_frame中每个观察的因子水平?

时间:2018-07-01 06:59:59

标签: r dataframe dplyr

我在R中有decimal d1 = 0.1f; //error decimal d2 = 1; //no error 个对象:

dataframe

现在我想添加第3列并将g的水平放到那里,我希望水平为dput(data_frame("n" = seq_len(10), "g" = sample(rep(factor(c("male", "female")), 5)))) structure(list(n = 1:10, g = structure(c(2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("female", "male"), class = "factor")), .Names = c("n", "g"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame" ))

我正在尝试使用:

1:length(unique(g))

没有运气,请告知我这里缺少什么?

这就是我想要的:

df %>% mutate(l = levels(g)))

1 个答案:

答案 0 :(得分:3)

有两种选择:

# Store levels as list in new column
dataframe %>% mutate(l = list(levels(g)))

# Store levels as separate by ',' in new column
dataframe %>% mutate(l = paste(levels(g), collapse=","))

# Just a column with number
dataframe %>% mutate(l = as.integer(g))

# # A tibble: 10 x 3
#       n g          l
#   <int> <fctr> <int>
# 1     1 male       2
# 2     2 male       2
# 3     3 female     1
# 4     4 female     1
# 5     5 female     1
# 6     6 female     1
# 7     7 female     1
# 8     8 male       2
# 9     9 male       2
# 10    10 male       2

由@DJV建议

#
df %>% mutate(l = paste(seq_along(levels(g)), collapse=","))