在r中的数据框中创建组

发布于 2025-02-03 06:00:16 字数 314 浏览 1 评论 0原文

您好,我有以下数据


n<-c(2,8,9,3,7,5,7,6,3,8,2,9,10,1)
tab<-data.frame("note"=n)

框到10组4,如下所示,如下所示,

”在此处输入图像说明”

hello i have the following dataframe


n<-c(2,8,9,3,7,5,7,6,3,8,2,9,10,1)
tab<-data.frame("note"=n)

I need to add a new column that classifies if the number is less than 3 it will be group 1 if it is greater than 5 it will be group 2 from 5 to 7 it will be group 3 and from 7 to 10 group 4 as shown below

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戴着白色围巾的女孩 2025-02-10 06:00:16

一个选项是使用case_when定义组:

library(dplyr)

tab %>%
  mutate(groups = case_when(note < 3 ~ 1,
                           note >= 3 & note < 7 ~ 2,
                           note == 7 ~ 3,
                           TRUE ~ 4))

或使用cut的其他选项:

tab %>% 
  mutate(groups = cut(tab$note, breaks = c(0, 2, 6, 7, 10), labels = 1:4))

output

   note groups
1     2     1
2     8     4
3     9     4
4     3     2
5     7     3
6     5     2
7     7     3
8     6     2
9     3     2
10    8     4
11    2     1
12    9     4
13   10     4
14    1     1

One option is to use case_when to define the groups:

library(dplyr)

tab %>%
  mutate(groups = case_when(note < 3 ~ 1,
                           note >= 3 & note < 7 ~ 2,
                           note == 7 ~ 3,
                           TRUE ~ 4))

Or another option using cut:

tab %>% 
  mutate(groups = cut(tab$note, breaks = c(0, 2, 6, 7, 10), labels = 1:4))

Output

   note groups
1     2     1
2     8     4
3     9     4
4     3     2
5     7     3
6     5     2
7     7     3
8     6     2
9     3     2
10    8     4
11    2     1
12    9     4
13   10     4
14    1     1
南风几经秋 2025-02-10 06:00:16

基本r(从Latemail和Andrewgb借用可重复使用的功能):

# Function to group the numeric data: 
# group_numeric_data => function()
group_numeric_data <- function(num_vec, break_points){
  
 # Compute the group values: group_vals => integer vector
 group_vals <- seq_along(break_points)[-length(break_points)]
 
 # Compute the groups: res => factor vector
  res <- cut(
    num_vec, 
    breaks = break_points, 
    labels = group_vals
  )

 # Explictly define returned object: factor vector => env
 return(res)
 
}

# Define the break points: break_points => numeric vector
break_points <- c(-Inf, 2, 6, 7, 10)

# Apply the function: groups => factor vector
tab$groups <- group_numeric_data(
  tab$note, 
  break_points
)

Base R (borrowing heavily from the latemail and AndrewGB) with a reusable function:

# Function to group the numeric data: 
# group_numeric_data => function()
group_numeric_data <- function(num_vec, break_points){
  
 # Compute the group values: group_vals => integer vector
 group_vals <- seq_along(break_points)[-length(break_points)]
 
 # Compute the groups: res => factor vector
  res <- cut(
    num_vec, 
    breaks = break_points, 
    labels = group_vals
  )

 # Explictly define returned object: factor vector => env
 return(res)
 
}

# Define the break points: break_points => numeric vector
break_points <- c(-Inf, 2, 6, 7, 10)

# Apply the function: groups => factor vector
tab$groups <- group_numeric_data(
  tab$note, 
  break_points
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文