r - Create new column with vector values based on groups from another character column
问题描述
I'm quite new to R and am currently stuck with my data.frame. I have a character column with different groups varying in numbers. For example the first seven rows being "A", the next five rows being "B" and so on. Now I have a vector with a length being equal to the total number of groups. My goal is to create a new column, where all "A" rows get the first vector value, all "B" rows the second value and so on.
I already tried:
values <- c("G", "H", "J", "K")
dat$col2 <- values[dat$col1]
from an earlier entry (Create new column based on 4 values in another column) and it worked. But after updating R it somehow doesn't work anymore. Though it creates the new column "col2", the values are now all NA and not corresponding the vector.
Can anyone help me out with that?
edit: example as reproducible code:
first_column <- c(rep("value_1", 6),rep("value_2",7))
df <- data.frame(first_column)
df$second_column <- c("A","B")[df$first_column]
解决方案
I think that you are simply looking for an ifelse
.
group.sizes <- c(10, 20, 30 , 40)
names(group.sizes) <- c("G", "H", "J", "K")
df$new.column <- ifelse(df$column == "G",
group.sizes["G"],
ifelse(df$column == "H",
group.sizes["H"],
ifelse(df$column == "J",
group.sizes["J"],
ifelse(df$column == "K",
group.sizes["K"],
NA)))
推荐阅读
- delphi - 在文件夹中搜索大量特定文件名
- anaconda - 使用 conda 成功安装 cgal 后出现 ModuleNotFound 错误?
- shell - 得到:意外的 URISyntaxException
- kubernetes - Kubernetes:通过 traefik 入口控制器处理集群中多个 LoadBalancer 的连接
- angular - 在 Angular 中迭代 *ngFor 时修复文字中的破折号
- php - PHP 处理超时。如何优化/排队以避免超时
- nginx - 工作进程的 Openresty 动态生成
- java - 在 Java 中读取 Docx 文件页数的最快方法(文字渲染后)?
- php - 通过 URL 传递多个查询参数
- node.js - 这与 npm 无法下载依赖项有关