gpt4 book ai didi

R:从一列中提取数据以创建新列

转载 作者:行者123 更新时间:2023-12-04 22:39:55 25 4
gpt4 key购买 nike

我有样本名称的数据需要解包并创建到新列中。

sample
P10.1
P11.2
S1.1
S3.3

使用样本 ID 数据,我需要创建三个新列:组织、植物、阶段。
sample tissue plant stage
P10.1 P 10 1
P11.2 P 11 2
S1.1 S 1 1
S3.3 S 3 3

有没有办法从示例列中提取数据来填充三个新列?

最佳答案

使用 dplyrtidyr .

首先我们插入一个“.”在示例代码中,接下来我们将示例分成 3 列。

library(dplyr)
library(tidyr)

df %>%
mutate(sample = paste0(substring(df$sample, 1, 1), ".", substring(df$sample, 2))) %>%
separate(sample, into = c("tissue", "plant", "stage"), remove = FALSE)

sample tissue plant stage
1 P.10.1 P 10 1
2 P.11.2 P 11 2
3 S.1.1 S 1 1
4 S.3.3 S 3 3

数据:
df <- structure(list(sample = c("P10.1", "P11.2", "S1.1", "S3.3")), 
.Names = "sample",
class = "data.frame",
row.names = c(NA, -4L))

关于R:从一列中提取数据以创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50474431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com