r删除并重命名str_split之后的列

时间:2016-08-24 14:47:41

标签: r rscript

我使用str_split_fixed将我的第一列拆分为另外6列。

adobe.placement$name <- str_split_fixed(adobe.placement$name, ":::", 6)

之后我想使用colnames重命名这些列:

colnames(adobe.placement) <- c("Source","Keyword","Type","Campaign","Medium","Visits")

它将前6列更改为Source.1,Source.2等。如何将这些列更改为specfic名称?我也无法使用以下方法删除其中一列:

adobe.placement[3] <- NULL

如何重命名这些拆分列并删除其中一列?

编辑:输入数据:

    name                                        A   B   C   D   E
2   test:::ok:::test:::ok:::test:::ok:::delete  2   3   4   ok  1900-01-05
3   test:::ok:::test:::ok:::test:::ok:::delete  2   2   4   ok  1900-01-05
4   test:::ok:::test:::ok:::test:::ok:::delete  2   2   4   ok  1900-01-05
5   test:::ok:::test:::ok:::test:::ok:::delete  2   2   4   ok  1900-01-05
6   test:::ok:::test:::ok:::test:::ok:::delete  2   2   4   ok  1900-01-05

期望的输出:

    Source  Keyword  Type  Campaign  Medium  Visits   A  B  C  D   E
2   test    ok       test  ok        test    ok       2  3  4  ok  1900-01-05
3   test    ok       test  ok        test    ok       2  3  4  ok  1900-01-05
4   test    ok       test  ok        test    ok       2  3  4  ok  1900-01-05
5   test    ok       test  ok        test    ok       2  3  4  ok  1900-01-05
6   test    ok       test  ok        test    ok       2  3  4  ok  1900-01-05

2 个答案:

答案 0 :(得分:0)

您的第一行代码:

adobe.placement$name <- str_split_fixed(adobe.placement$name, ":::", 6)

将整个数据框(6列)分配到原始数据框的单个列中,实际上是嵌套的数据框。然后从外部列名称中为列提供名称,这些名称与内部列名称连接,用&#34;分隔。&#34;正如你所见。发现奇怪事件的线索是,如果您输入str(adobe.placement)并看到data.frame包含另一个data.frame

解决方案是避免陷入这种混乱:

adobe.placement <- cbind(str_split_fixed(adobe.placement$name, ":::", 6),adobe.placement[,-1])
names(adobe.placement)[1:6] <- c("Source","Keyword","Type","Campaign","Medium","Visits")

将额外的列添加到数据框中,而不是将它们放在现有列中。

答案 1 :(得分:0)

注意:这个答案类似于另一个答案,但可能更完整(即提供剪切/粘贴时有用的数据和代码)。如果这被认为过于接近答案或其他答案更加完整,我将删除此答案;如果没有,我会删除此笔记。

您可以执行以下操作

### Input data
df <- structure(list(name = c("test:::ok:::test:::ok:::test:::ok:::delete", 
"test:::ok:::test:::ok:::test:::ok:::delete", "test:::ok:::test:::ok:::test:::ok:::delete", 
"test:::ok:::test:::ok:::test:::ok:::delete", "test:::ok:::test:::ok:::test:::ok:::delete"
), A = c(2L, 2L, 2L, 2L, 2L), B = c(3L, 2L, 2L, 2L, 2L), C = c(4L, 
4L, 4L, 4L, 4L), D = c("ok", "ok", "ok", "ok", "ok"), E = c("1900-01-05", 
"1900-01-05", "1900-01-05", "1900-01-05", "1900-01-05")), .Names = c("name", 
"A", "B", "C", "D", "E"), class = "data.frame", row.names = c(NA, 
-5L))

new_colnames <- c("Source", "Keyword", "Type", "Campaign", "Medium", "Visits")
colcnt <- length(new_colnames)
### Even if 'name' has extra ':::' trailing, the result should be ok.
newdf  <- cbind(str_split_fixed(df$name, ":::", colcnt+1)[,-(colcnt+1)], df[,-1])
colnames(newdf)[1:colcnt] <- new_colnames
newdf

##   Source Keyword Type Campaign Medium Visits A B C  D          E
## 1   test      ok test       ok   test     ok 2 3 4 ok 1900-01-05
## 2   test      ok test       ok   test     ok 2 2 4 ok 1900-01-05
## 3   test      ok test       ok   test     ok 2 2 4 ok 1900-01-05
## 4   test      ok test       ok   test     ok 2 2 4 ok 1900-01-05
## 5   test      ok test       ok   test     ok 2 2 4 ok 1900-01-05