从多种不同格式创建标准化数据表条目

发布于 2024-12-10 18:24:22 字数 379 浏览 0 评论 0原文

我有一个包含多个字段的数据框。这些字段之一是“样本”,由于输入多种多样,我的样本使用多种格式命名。以下是一些示例:

 "12" "250" "1248" "1_100111" "16_100111" "125_081811" "1249_100111" 

上面的示例代表了大多数示例。我想将所有样本更改为 4 位数字格式,以便可以轻松对它们进行排序。上述示例的最终结果将是:

 "0012" "0250" "1248" "0001" "0016" "0125" "1249" 

因此,在某些情况下必须添加零,而在其他情况下必须删除日期标记。 非常重要的是,更改是在数据框的上下文中进行的,并以相同的格式返回。

I have a data frame that contains a number of fields. One of these fields is "Sample" and due to a variety of inputs my samples are named using a variety of formats. Here are some examples:

 "12" "250" "1248" "1_100111" "16_100111" "125_081811" "1249_100111" 

The above examples represent the majority of the samples. I would like to change all of the samples to a 4 digit format so they can be easily sorted. The final result of the above examples would be:

 "0012" "0250" "1248" "0001" "0016" "0125" "1249" 

Thus, in some cases zeros must be added and in other cases, the date marker must be cut off.
It is very important that the changes are made within the context of a data frame and returned in the same format.

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

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

发布评论

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

评论(2

半山落雨半山空 2024-12-17 18:24:22

干得好:

x <- c("12", "250", "1248", "1_100111", "16_100111", "125_081811", "1249_100111")
sprintf(as.numeric(gsub("(\\d*)_*\\d*$", "\\1", x)), fmt="%04d")

[1] "0012" "0250" "1248" "0001" "0016" "0125" "1249"

Here you go:

x <- c("12", "250", "1248", "1_100111", "16_100111", "125_081811", "1249_100111")
sprintf(as.numeric(gsub("(\\d*)_*\\d*$", "\\1", x)), fmt="%04d")

[1] "0012" "0250" "1248" "0001" "0016" "0125" "1249"
转身以后 2024-12-17 18:24:22
sprintf("%04s",
  sub("_.+", "", c("12", "250", "1248", "1_100111", "16_100111", 
                   "125_081811", "1249_100111" ) )
[1] "0012" "0250" "1248" "0001" "0016" "0125" "1249"
sprintf("%04s",
  sub("_.+", "", c("12", "250", "1248", "1_100111", "16_100111", 
                   "125_081811", "1249_100111" ) )
[1] "0012" "0250" "1248" "0001" "0016" "0125" "1249"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文