dplyr: how to use mutate function to make a key combining a prefix string and a sequence of values

时间:2022-02-05 20:29:07

Working on a 4 rows data.frame object with dplyr, I want to created a new "id" column combining a prefix string and a sequence of values.

使用dplyr处理4行data.frame对象时,我想创建一个新的“id”列,其中包含前缀字符串和值序列。

What I expected:

我的期望:

 columnA|columnB|columnC|id
 data    data    data    id-1
 data    data    data    id-2
 data    data    data    id-3
 data    data    data    id-4

What I tried:

我尝试了什么:

 library (dplyr)
 y <- x %>%
        mutate (id =  "id- " & seq(from = 1, to =4, by = 1))

2 个解决方案

#1


6  

Either:

x %>% mutate(id = paste0('id-', 1:4))

or:

x %>% mutate(id = paste0('id-', row_number()))

or:

x %>% mutate(id = paste0('id-', 1:n()))

gives you what you want:

给你你想要的东西:

  columnA columnB columnC   id
1    data    data    data id-1
2    data    data    data id-2
3    data    data    data id-3
4    data    data    data id-4

Off course this can also be accomplished easily in base R:

当然,这也可以在基础R中轻松完成:

x$id <- paste0('id-', 1:nrow(x))

It is better not to use row.names because they are not always numbers (see for example row.names(mtcars)).

最好不要使用row.names,因为它们并不总是数字(例如参见row.names(mtcars))。


Or using the data.table package:

或者使用data.table包:

library(data.table)
setDT(x)[, id := paste0('id-',.I)]

#2


4  

You can do this one of two ways (of many I assume):

你可以用两种方式中的一种(我假设的很多方式):

1) Using dplyr as follows:

1)使用dplyr如下:

df %>% mutate(id = paste0('id-', row_number()))
  columnA columnB columnC   id
1    data    data    data id-1
2    data    data    data id-2
3    data    data    data id-3
4    data    data    data id-4

2) Using simple base package assignment

2)使用简单的基础包分配

df$id <- paste0('id-', row.names(df))
df
  columnA columnB columnC   id
1    data    data    data id-1
2    data    data    data id-2
3    data    data    data id-3
4    data    data    data id-4

#1


6  

Either:

x %>% mutate(id = paste0('id-', 1:4))

or:

x %>% mutate(id = paste0('id-', row_number()))

or:

x %>% mutate(id = paste0('id-', 1:n()))

gives you what you want:

给你你想要的东西:

  columnA columnB columnC   id
1    data    data    data id-1
2    data    data    data id-2
3    data    data    data id-3
4    data    data    data id-4

Off course this can also be accomplished easily in base R:

当然,这也可以在基础R中轻松完成:

x$id <- paste0('id-', 1:nrow(x))

It is better not to use row.names because they are not always numbers (see for example row.names(mtcars)).

最好不要使用row.names,因为它们并不总是数字(例如参见row.names(mtcars))。


Or using the data.table package:

或者使用data.table包:

library(data.table)
setDT(x)[, id := paste0('id-',.I)]

#2


4  

You can do this one of two ways (of many I assume):

你可以用两种方式中的一种(我假设的很多方式):

1) Using dplyr as follows:

1)使用dplyr如下:

df %>% mutate(id = paste0('id-', row_number()))
  columnA columnB columnC   id
1    data    data    data id-1
2    data    data    data id-2
3    data    data    data id-3
4    data    data    data id-4

2) Using simple base package assignment

2)使用简单的基础包分配

df$id <- paste0('id-', row.names(df))
df
  columnA columnB columnC   id
1    data    data    data id-1
2    data    data    data id-2
3    data    data    data id-3
4    data    data    data id-4