Craig
Craig

Reputation: 513

Splitting Dataframe Rows And Renaming Columns

If I have the following dataframe...

  X1      X2
1 a=12254 b=7052862
2 a=12130 b=7052862
3  a=7884 b=7052862
4   a=841 b=7052862
5  a=3486 b=7052862
6 a=11986 b=7052862

How can I transform it to the following.

a     b
1 12254 7052862
2 12130 7052862
3  7884 7052862
4   841 7052862
5  3486 7052862
6 11986 7052862

Upvotes: 1

Views: 119

Answers (2)

bramtayl
bramtayl

Reputation: 4024

library(dplyr)
library(tidyr)

test = 
  data_frame(
    X1 = c("a=12254" ,
           "a=12130",
           "a=7884",
           "a=841",
           "a=3486",
           "a=11986"),
    X2 = c(
      "b=7052862",
      "b=7052862",
      "b=7052862",
      "b=7052862",
      "b=7052862",
      "b=7052862"))

result = 
  test %>%
  mutate(ID = 1:n()) %>%
  gather(variable, value, -ID) %>%
  select(-variable) %>%
  separate(value, c("new_variable", "number"), sep = "=") %>%
  spread(new_variable, number)

Upvotes: 1

jeremycg
jeremycg

Reputation: 24945

You can use the function extract_numeric available in tidyr, and apply it to each column:

library(tidyr)
names <- lapply(dat, function(x) strsplit(as.character(x[[1]]), "\\=")[[1]][1])
dat <- as.data.frame(apply(dat, 2, extract_numeric))
names(dat) <- unlist(names)
dat

      a       b
1 12254 7052862
2 12130 7052862
3  7884 7052862
4   841 7052862
5  3486 7052862
6 11986 7052862

Upvotes: 2

Related Questions