gpt4 book ai didi

regex - 如何将单列中的内容拆分为 R 中的两个单独的列?

转载 作者:行者123 更新时间:2023-12-02 07:26:16 25 4
gpt4 key购买 nike

我的数据框中有一列:

Colname
20151102
19920311
20130204
>=70
60-69
20-29

我想将这一列分成两列,例如:

Col1         Col2
20151102
19920311
20130204
>=70
60-69
20-29

我怎样才能达到这个结果?

最佳答案

不需要任何包:

df[,c("Col1", "Col2")] <- ""

isnum <- suppressWarnings(!is.na(as.numeric(df$colname)))

df$Col1[isnum] <- df$colname[isnum]
df$Col2[!isnum] <- df$colname[!isnum]

df <- df[,!(names(df) %in% "colname")]

数据:

df = data.frame(colname=c("20151102","19920311","20130204",">=70","60-69","20-29"), stringsAsFactors=FALSE)

关于regex - 如何将单列中的内容拆分为 R 中的两个单独的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28317155/

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