亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

累積粘貼(連接)由另一個變量分組的值

累積粘貼(連接)由另一個變量分組的值

大話西游666 2019-12-06 11:17:28
我在處理R中的數據幀時遇到問題。我想根據另一列中單元格的值將不同行中單元格的內容粘貼到一起。我的問題是我希望輸出逐漸(累積)打印。輸出向量的長度必須與輸入向量的長度相同。這是一個與我正在處理的樣本表相似的樣本表:id <- c("a", "a", "a", "b", "b", "b")content <- c("A", "B", "A", "B", "C", "B")(testdf <- data.frame(id, content, stringsAsFactors=FALSE))#  id content#1  a       A#2  a       B#3  a       A#4  b       B#5  b       C#6  b       B這就是我希望結果看起來像這樣:result <- c("A", "A B", "A B A", "B", "B C", "B C B") result#[1] "A"     "A B"   "A B A" "B"     "B C"   "B C B"我不需要這樣的東西:ddply(testdf, .(id), summarize, content_concatenated = paste(content, collapse = " "))#  id content_concatenated#1  a                A B A#2  b                B C B
查看完整描述

3 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

您可以使用以下命令定義“累積粘貼”功能Reduce:


cumpaste = function(x, .sep = " ") 

          Reduce(function(x1, x2) paste(x1, x2, sep = .sep), x, accumulate = TRUE)


cumpaste(letters[1:3], "; ")

#[1] "a"       "a; b"    "a; b; c"

Reduce的循環避免了從一開始就重新串聯元素,因為它通過下一個元素延長了先前的串聯。


按組應用:


ave(as.character(testdf$content), testdf$id, FUN = cumpaste)

#[1] "A"     "A B"   "A B A" "B"     "B C"   "B C B"

另一個想法是,可以在開始時依次連接整個向量,然后逐步地substring:


cumpaste2 = function(x, .sep = " ")

{

    concat = paste(x, collapse = .sep)

    substring(concat, 1L, cumsum(c(nchar(x[[1L]]), nchar(x[-1L]) + nchar(.sep))))

}

cumpaste2(letters[1:3], " ;@-")

#[1] "a"           "a ;@-b"      "a ;@-b ;@-c"

這似乎也更快一些:


set.seed(077)

X = replicate(1e3, paste(sample(letters, sample(0:5, 1), TRUE), collapse = ""))

identical(cumpaste(X, " --- "), cumpaste2(X, " --- "))

#[1] TRUE

microbenchmark::microbenchmark(cumpaste(X, " --- "), cumpaste2(X, " --- "), times = 30)

#Unit: milliseconds

#                  expr      min       lq     mean   median       uq      max neval cld

#  cumpaste(X, " --- ") 21.19967 21.82295 26.47899 24.83196 30.34068 39.86275    30   b

# cumpaste2(X, " --- ") 14.41291 14.92378 16.87865 16.03339 18.56703 23.22958    30  a

...使其成為cumpaste_faster。


查看完整回答
反對 回復 2019-12-06
?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

您也可以嘗試 dplyr


 library(dplyr)

 res <- testdf%>%

        mutate(n=row_number()) %>%

        group_by(id) %>%

        mutate(n1=n[1L]) %>%

        rowwise() %>% 

        do(data.frame(cont_concat= paste(content[.$n1:.$n],collapse=" "),stringsAsFactors=F))


 res$cont_concat

 #[1] "A"     "A B"   "A B A" "B"     "B C"   "B C B"


查看完整回答
反對 回復 2019-12-06
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

這是ddply一種使用sapply和子集逐步粘貼在一起的方法:


library(plyr)

ddply(testdf, .(id), mutate, content_concatenated = sapply(seq_along(content), function(x) paste(content[seq(x)], collapse = " ")))

  id content content_concatenated

1  a       A                    A

2  a       B                  A B

3  a       A                A B A

4  b       B                    B

5  b       C                  B C

6  b       B                B C B


查看完整回答
反對 回復 2019-12-06
  • 3 回答
  • 0 關注
  • 702 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號