![](/img/trans.png)
[英]How can I assign a column a character Value based on whether another column meets a condition in R?
[英]How to assign a value for a column based on another column value in R?
我有一个数据帧
df <- data.frame(structure(list(col1= c("A", "B", "C", "D", "A"),
col2= c(1, 1, 1, 1, 5), col3 = c(2L, 1L, 1L, 1L, 1L)),
.Names = c("col1", "col2", "col3"),
row.names = c(NA, -5L), class = "data.frame"))
我想添加额外的列col4,其值基于col2。 col2中具有相同值的行在col4中也具有相同的值。
通过解决方法,我以下列方式生成结果。
x <- df[!duplicated(df$col2),]
x$col4 <- paste("newValue", seq(1:nrow(x)), sep="_")
df_new <- merge(x, df, by ="col2")
df_new <- df_new[,c("col2","col4", "col1.y", "col3.y")]
这有效,但我认为这样做有更好的方法。 谢谢!
你可以从dplyr
尝试dense_rank()
:
library(dplyr)
df %>%
mutate(col4 = dense_rank(col2),
col4_new = paste0("newValue_", col4))
这给出了与你想要的输出非常相似的东西,但我不确定你到底想要什么。 如果要确保col2
具有相同值的所有行在col4
获得相同的值,则只需arrange
df
然后使用dense_rank
:
df %>%
arrange(col2) %>%
mutate(col4 = dense_rank(col2),
col4_new = paste0("newValue_", col4))
这适用于任意大小的data.frame
。
可能这有帮助
df$col4 <- paste0("newValue_", cumsum(!duplicated(df$col2)))
df$col4
#[1] "newValue_1" "newValue_1" "newValue_1" "newValue_1" "newValue_2"
或者我们使用match
with(df, paste0("newValue_", match(col2, unique(col2))))
#[1] "newValue_1" "newValue_1" "newValue_1" "newValue_1" "newValue_2"
或者它可以用factor
完成
with(df, paste0("newValue_", as.integer(factor(col2, levels = unique(col2)))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.