gpt4 book ai didi

r - 通过合并数据框中以 "-"分隔的多个列来创建新列

转载 作者:行者123 更新时间:2023-12-01 22:05:42 24 4
gpt4 key购买 nike

如何通过在数据框中组合多个列以及用“-”分隔的元素来创建新列。

df = read.table(text = "

chr start end CNA
1 68580000 68640000 loss
3 15360000 16000000 loss
4 122660000 123500000 gain
7 48320000 48400000 loss

", sep = "", header = TRUE)

期望的输出是:

chr  start       end        CNA       sample.ID
1 68580000 68640000 loss 1-68580000-68640000
3 15360000 16000000 loss 3-15360000-16000000
4 122660000 123500000 gain 4-122660000-123500000
7 48320000 48400000 loss 7-48320000-48400000

我试过了

cols <- c("ID","seqnames","start")
df$sample.id <- do.call(paste,rbind(final[cols]))

我不知道如何用“-”分隔新列中的字符。

最佳答案

你可以试试

cols <- c("chr", "start", "end")
df$sample.id <- do.call(paste, c(df[cols], sep="-"))
df
# chr start end CNA sample.id
#1 1 68580000 68640000 loss 1-68580000-68640000
#2 3 15360000 16000000 loss 3-15360000-16000000
#3 4 122660000 123500000 gain 4-122660000-123500000
#4 7 48320000 48400000 loss 7-48320000-48400000

或者

do.call(sprintf, c(df[cols], fmt='%s-%s-%s'))

基准

set.seed(24)
df1 <- as.data.frame(matrix(sample(0:1000, 3*1e6, replace=TRUE), ncol=3))

akrun1 <- function() {do.call(paste, c(df1, sep="-"))}
akrun2 <- function() {do.call(sprintf, c(df1, fmt='%s-%s-%s'))}
Mamoun <- function() {apply(df1, 1, paste0, collapse="-")}
library(microbenchmark)
microbenchmark(akrun1(), akrun2(), Mamoun(), unit='relative', times=20L)
#Unit: relative
# expr min lq mean median uq max neval cld
#akrun1() 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 20 a
#akrun2() 1.003460 1.034647 1.053498 1.052192 1.048754 1.076809 20 a
#Mamoun() 6.706523 7.460565 6.657434 6.626459 6.416989 6.105361 20 b

数据

df <- structure(list(chr = c(1L, 3L, 4L, 7L), start = c(68580000L, 
15360000L, 122660000L, 48320000L), end = c(68640000L, 16000000L,
123500000L, 48400000L), CNA = c("loss", "loss", "gain", "loss"
)), .Names = c("chr", "start", "end", "CNA"), class = "data.frame",
row.names = c(NA, -4L))

关于r - 通过合并数据框中以 "-"分隔的多个列来创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29727309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com