字母数字到r的数量增加

发布于 2025-02-01 03:16:40 字数 91 浏览 3 评论 0原文

我在r中有一张iD列,例如:'1a','2a','1b','2b'等。

我想将它们转换为:1、2、3、4,,等等。

我感谢任何建议。 谢谢!

I have a table in R with a column of ID's like: '1A', '2A', '1B', '2B', etc.

I would like to convert them to numbers such as: 1, 2, 3, 4, etc.

I would appreciate any suggestion.
Thanks!

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

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

发布评论

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

评论(2

記憶穿過時間隧道 2025-02-08 03:16:40

另一种方法是转换为一个因素,然后转换为数字。这样就没有顺序顺序的假设。

df <- data.frame(list(
  IDs=c('1A', '2A', '1B', '2B'),
  vals=c(8,8,8,8)
))

df$IDs <- as.numeric(as.factor(df$IDs))

Another way is to convert to a factor, and then to numbers. that way there is no assumptions of sequential order.

df <- data.frame(list(
  IDs=c('1A', '2A', '1B', '2B'),
  vals=c(8,8,8,8)
))

df$IDs <- as.numeric(as.factor(df$IDs))
平定天下 2025-02-08 03:16:40
df <- data.frame(list(
  IDs=c('1A', '2A', '1B', '2B'),
  vals=c(8,8,8,8)
))

假设ID是按顺序

df$IDs <- seq(1,nrow(df))

排序

df$IDs <- seq_along(df$IDs)

  IDs vals
1   1    8
2   2    8
3   3    8
4   4    8

的使用row_number

library(tidyverse)
df <- df %>% mutate(IDs = row_number())
df <- data.frame(list(
  IDs=c('1A', '2A', '1B', '2B'),
  vals=c(8,8,8,8)
))

assuming that the IDs are in sequential order replace the IDs vector with a sequence of numbers starting at 1 and ending at the length of the data frame

df$IDs <- seq(1,nrow(df))

alternatively:

df$IDs <- seq_along(df$IDs)

  IDs vals
1   1    8
2   2    8
3   3    8
4   4    8

The tidyverse solution would be to use row_number

library(tidyverse)
df <- df %>% mutate(IDs = row_number())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文