0

我想在数据框中创建一个变量,该变量将根据列的四分位数/中位数对观察结果进行分类。

以下是我尝试过的。

Name<-c("name1","name2","name3","name4","name5","name6")
Age<-c(49,12,29,55,25,19)

df9<-data.frame(Name,Age)

df9$catoG[df9$Age<=quantile(df9$Age,0.25)]<-"Young"
df9$catoG[df9$Age>quantile(df9$Age,0.25) & df9$Age<=median(df9$Age)]<-"Adult"
df9$catoG[df9$Age>median(df9$Age)]<-"Elder"

我收到的输出是

   Name Age catoG
1 name1  49 Elder
2 name2  12 Young
3 name3  29 Elder
4 name4  55 Elder
5 name5  25 Adult
6 name6  19 Young

R中是否有更有效的方法可以实现相同的目标?

4

3 回答 3

1

cut是所有涉及在范围内分割向量的任务的朋友:

df9$new = cut(df9$Age, 
              breaks = c(-Inf, quantile(df9$Age,c(0.25, 0.5)), Inf), 
              labels = c('Young', 'Adult', 'Elder') )

#   Name Age catoG   new
#1 name1  49 Elder Elder
#2 name2  12 Young Young
#3 name3  29 Elder Elder
#4 name4  55 Elder Elder
#5 name5  25 Adult Adult
#6 name6  19 Young Young  
于 2019-03-14T17:53:41.797 回答
0

您可以从包 中使用dplyr::mutate和:dplyr::case_whendplyr

Name<-c("name1","name2","name3","name4","name5","name6")
Age<-c(49,12,29,55,25,19)

df9<-data.frame(Name,Age)

df9 %>% mutate(catoG = case_when(Age<=quantile(Age,0.25) ~ 'Young',
                                 Age>quantile(Age,0.25) & Age<=median(Age) ~ 'Adult',
                                 TRUE ~ 'Elder'))
于 2019-03-14T17:47:37.050 回答
0

以下函数从数值向量创建一个 quantile(n) 组向量(因此 n = 4 表示四分位数):

qgroup = function(numvec, n = 4){

  qtile = quantile(numvec, probs = seq(0, 1, 1/n))
  out = sapply(numvec, function(x) sum(x >= qtile[-(n+1)]))

  return(out)
}

将函数应用于您的数据:

Name = c("name1","name2","name3","name4","name5","name6")
Age = c(49,12,29,55,25,19)

df9 = data.table(Name,Age)
df9[, Q := qgroup(Age)]

> df9
    Name Age Q
1: name1  49 4
2: name2  12 1
3: name3  29 3
4: name4  55 4
5: name5  25 2
6: name6  19 1

最后,我们标记四分位数组:

labels = list('Young', 'Adult', 'Elder', 'Elder')

df9[, Label := sapply(Q, function(x) labels[x])]
> df9
    Name Age Q Label
1: name1  49 4 Elder
2: name2  12 1 Young
3: name3  29 3 Elder
4: name4  55 4 Elder
5: name5  25 2 Adult
6: name6  19 1 Young
于 2019-09-26T13:21:28.193 回答