简体   繁体   English

如何根据R中的另一个列值为列分配值?

[英]How to assign a value for a column based on another column value in R?

I have a dataframe 我有一个数据帧

 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"))

I want to add additional column, col4 with values based on col2. 我想添加额外的列col4,其值基于col2。 Rows that have the same value in col2 will have the same value in col4 as well. col2中具有相同值的行在col4中也具有相同的值。

With a work around, I generated a result in the following way. 通过解决方法,我以下列方式生成结果。

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")]

This works but I thought there is a better way doing this. 这有效,但我认为这样做有更好的方法。 Thank you! 谢谢!

You could try dense_rank() from dplyr : 你可以从dplyr尝试dense_rank()

library(dplyr)
df %>% 
    mutate(col4 = dense_rank(col2),
           col4_new = paste0("newValue_", col4))

This gives something very similar to your desired output in your question, but I'm not sure exactly what you're looking for. 这给出了与你想要的输出非常相似的东西,但我不确定你到底想要什么。 If you want to ensure that all rows with identical values in col2 get the same value in col4 then just arrange the df and then use dense_rank : 如果要确保col2具有相同值的所有行在col4获得相同的值,则只需arrange df然后使用dense_rank

df %>% 
    arrange(col2) %>% 
    mutate(col4 = dense_rank(col2),
           col4_new = paste0("newValue_", col4))

This should work for a data.frame of arbitrary size. 这适用于任意大小的data.frame

May be this helps 可能这有帮助

df$col4 <- paste0("newValue_", cumsum(!duplicated(df$col2)))
df$col4
#[1] "newValue_1" "newValue_1" "newValue_1" "newValue_1" "newValue_2"

Or we use match 或者我们使用match

with(df, paste0("newValue_", match(col2, unique(col2))))
#[1] "newValue_1" "newValue_1" "newValue_1" "newValue_1" "newValue_2"

Or it can be done with factor 或者它可以用factor完成

with(df, paste0("newValue_", as.integer(factor(col2, levels = unique(col2)))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM