第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將數(shù)據(jù)框架字符串列拆分為多個(gè)列

將數(shù)據(jù)框架字符串列拆分為多個(gè)列

將數(shù)據(jù)框架字符串列拆分為多個(gè)列我想要獲取表格的數(shù)據(jù)before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))  attr          type1    1   foo_and_bar2   30 foo_and_bar_23    4   foo_and_bar4    6 foo_and_bar_2并使用split()上面的列“ type”來(lái)得到這樣的東西:  attr type_1 type_21    1    foo    bar2   30    foo  bar_23    4    foo    bar4    6    foo  bar_2我提出了一些令人難以置信的復(fù)雜問(wèn)題,涉及某種形式的apply工作,但我已經(jīng)錯(cuò)了。這似乎太復(fù)雜了,不是最好的方式。我可以使用strsplit如下,但不清楚如何將其恢復(fù)到數(shù)據(jù)框中的2列。> strsplit(as.character(before$type),'_and_')[[1]][1] "foo" "bar"[[2]][1] "foo"   "bar_2"[[3]][1] "foo" "bar"[[4]][1] "foo"   "bar_2"謝謝你的任何指示。我還沒(méi)有完全理解R列表。
查看完整描述

4 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

另一種選擇是使用新的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_")


##   attr foo   bar

## 1    1 foo   bar

## 2   30 foo bar_2

## 3    4 foo   bar

## 4    6 foo bar_2


查看完整回答
反對(duì) 回復(fù) 2019-05-28
?
qq_花開花謝_0

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊

加入強(qiáng)制性data.table解決方案


library(data.table) ## v 1.9.6+ 

setDT(before)[, paste0("type", 1:2) := tstrsplit(type, "_and_")]

before

#    attr          type type1 type2

# 1:    1   foo_and_bar   foo   bar

# 2:   30 foo_and_bar_2   foo bar_2

# 3:    4   foo_and_bar   foo   bar

# 4:    6 foo_and_bar_2   foo bar_2

我們也可以通過(guò)添加和參數(shù)來(lái)確保生成的列具有正確的類型并提高性能(因?yàn)樗皇钦嬲恼齽t表達(dá)式)type.convertfixed"_and_"


setDT(before)[, paste0("type", 1:2) := tstrsplit(type, "_and_", type.convert = TRUE, fixed = TRUE)]


查看完整回答
反對(duì) 回復(fù) 2019-05-28
  • 4 回答
  • 0 關(guān)注
  • 762 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)