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

Lau*_*ard 4 r dataframe

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

sample
P10.1
P11.2
S1.1
S3.3
Run Code Online (Sandbox Code Playgroud)

使用样本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
Run Code Online (Sandbox Code Playgroud)

有没有办法从样本列中提取数据以填充三个新列?

phi*_*ver 6

使用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
Run Code Online (Sandbox Code Playgroud)

数据:

df <- structure(list(sample = c("P10.1", "P11.2", "S1.1", "S3.3")), 
                .Names = "sample", 
                class = "data.frame", 
                row.names = c(NA, -4L))
Run Code Online (Sandbox Code Playgroud)