-1

I used str_split_fixed to split my first column to 6 others.

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

After that I wanted to rename those columns using colnames:

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

It changed first 6 columns to Source.1, Source.2 etc. How can I change these columns to specfic names? I also couldn't remove one of these columns using:

adobe.placement[3] <- NULL

How can I rename those splitted columns and remove one of them?

EDIT: input data:

    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

Desired output:

    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
TityBoi
  • 399
  • 1
  • 4
  • 11
  • Please provide sample input data. You can do this by cutting/pasting the results of `dput`. – steveb Aug 24 '16 at 14:52
  • Also, even if it seems obvious what the desired output should be, it is helpful if you include the desired output. – steveb Aug 24 '16 at 15:01
  • I just edited my post with input data. – TityBoi Aug 24 '16 at 15:01
  • Using `dput` to provide input data enables other to just cut/paste the data to load in R w/o having to do any manual manipulation of formatted data. – steveb Aug 24 '16 at 15:04

2 Answers2

0

Your first line of code:

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

assigns a whole dataframe (6 columns) into a single column of your original dataframe, effectively a nested dataframe. The columns are then given names from the outer column name concatenated with the inner column name, separated by "." as you've seen. The clue that something strange has happened is if you type str(adobe.placement) and see that the data.frame contains another data.frame.

The solution is to avoid getting into this mess with something like:

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")

which adds extra columns to the dataframe, rather than putting them inside an existing column.

Miff
  • 7,486
  • 20
  • 20
  • Why is `str_split_fixed` being called with a value of "2" ? I may be missing something. – steveb Aug 24 '16 at 15:34
  • Thanks @steveb left in from testing before op gave an example dataset. – Miff Aug 24 '16 at 16:00
  • Please see my note at the beginning of my answer as I don't know if these are too similar (i.e. if I should delete my answer). I was attempting to provide a complete cut/paste solution. If it is too similar I will delete it and feel free to add anything I have to your answer. – steveb Aug 24 '16 at 16:05
0

NOTE: this answer is similar to another answer, but likely more complete (i.e. providing data and code that works when cut/paste). If this is considered too close of an answer or the other answer is made more complete, I will delete this answer; if not, I will delete this note.

You could do something like the following

### 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
steveb
  • 5,382
  • 2
  • 27
  • 36