R中根據匹配原則將一列拆分為幾列的方法


例如我們需要將一下數據的第二列從and處拆分為兩列:

before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))

 attr          type
1    1   foo_and_bar
2   30 foo_and_bar_2
3    4   foo_and_bar
4    6 foo_and_bar_2

==>

  attr type_1 type_2
1    1    foo    bar
2   30    foo  bar_2
3    4    foo    bar
4    6    foo  bar_2
  1. 使用stringr包的str_split_fixed函數
library(stringr)
str_split_fixed(before$type, "_and_", 2)
  1. 使用do.call函數 (do.call(what, args, quote = FALSE, envir = parent.frame())
before <- data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))  
out <- strsplit(as.character(before$type),'_and_') 
do.call(rbind, out)
  1. 使用tidyr包
library(dplyr)
library(tidyr)
before <- data.frame(attr = c(1, 30 ,4 ,6 ), type = c('foo_and_bar', 'foo_and_bar_2'))
before %>% separate(type, c("foo", "bar"), "_and_")
  1. 使用sapply 以及 "["
before$type_1 < sapply(strsplit(as.character(before$type),'_and_'), "[", 1)
before$type_2 < sapply(strsplit(as.character(before$type),'_and_'), "[", 2)

或者

before <- data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
after <- with(before, data.frame(attr = attr))
after <- cbind(after, data.frame(t(sapply(out, `[`))))
names(after)[2:3] <- paste("type", 1:2, sep = "_")
  1. 使用unlist后重新划分矩陣
before <- data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
tmp <- matrix(unlist(strsplit(as.character(before$type), '_and_')), ncol=2,byrow=TRUE) #you should show how many columns you would get after spliting
after <- cbind(before$attr, as.data.frame(tmp))
names(after) <- c("attr", "type_1", "type_2")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM