表格如下所示,SampleID
和Number
是列名:
SampleID Number
G2Day2 15
G2Day4 11
G11Day13 5
我想使用SampleID
中的值创建另一列,因此新表格如下所示:
SampleID Group Day Number
G2Day2 G2 2 15
G2Day4 G2 4 11
G11Day13 G11 13 5
答案 0 :(得分:5)
您可以使用strsplit
将结果列表中的sampleID
和rbind
分开。
df[, c("Group", "Day")] <- do.call(rbind, strsplit(as.character(df[, "SampleID"]), "Day"))
df[, c("SampleID", "Group", "Day", "Number")]
## SampleID Group Day Number
## 1 G2Day2 G2 2 15
## 2 G2Day4 G2 4 11
## 3 G11Day13 G11 13 5
答案 1 :(得分:4)
gsub
可以很容易地完成这项特殊任务。试试这个:
df$Group <- gsub(pattern = "Day.*", replacement = "", x=df$SapmleID)
df$Number <- gsub(pattern = ".*Day", replacement = "", x=df$SampleID)
。*是一个表示任意数量字符的正则表达式
答案 2 :(得分:2)
这是一个tidyr
解决方案
library(tidyr)
cbind(df[1], separate(df, SampleID, c("Group", "Day"), "Day"))
# SampleID Group Day Number
# 1 G2Day2 G2 2 15
# 2 G2Day4 G2 4 11
# 3 G11Day13 G11 13 5
或类似于data.table
v >= 1.9.5
library(data.table) # v 1.9.5
setDT(df)[, c("Group", "Day") := tstrsplit(as.character(SampleID), "Day")][]
# SampleID Number Group Day
# 1: G2Day2 15 G2 2
# 2: G2Day4 11 G2 4
# 3: G11Day13 5 G11 13
答案 3 :(得分:1)
带一点正则表达式:
dat <- read.table(header = TRUE, text="SampleID Number
G2Day2 15
G2Day4 11
G11Day13 5")
within(dat, {
Group <- gsub('Day\\d+', '', SampleID)
Day <- gsub('.*Day(.*)', '\\1', SampleID)
})
# SampleID Number Day Group
# 1 G2Day2 15 2 G2
# 2 G2Day4 11 4 G2
# 3 G11Day13 5 13 G11
答案 4 :(得分:-2)
如果您的新表已有列名 这样的事情就是你要做的。 伪代码在这里:
foreach(Datarow row in myoriginaltable.rows)
{
int i = 0;
// Get an index reference to the row you create and add to the new table
i = myNewTable.AddRow(new DataRow);
myNewTable.Rows[i].Column["Number"] = row.Column["Number"];
// where ColumnName is the name of your column that you want to transfer from one to the other.
myNewTable.Rows[i].Column["ColumnName"] = row.Column["ColumnName"];
// Create a Column , give it a value from the row and Column["ColumnName"]
DataColumn dc = new DataColumn(row.Column["ColumnName"].Value.ToString());
myNewTable.Columns.Add(dc);
}
</code></pre>