拆分列和名称中的字符

发布于 2024-12-13 07:33:13 字数 738 浏览 0 评论 0原文

我想分割字符。虽然我有一个很大的数据框要工作,但下面的小例子展示了需要做什么。

  mydf <- data.frame (name = c("L1", "L2", "L3"), 
    M1 = c("AC", "AT", NA), M2 = c("CC", "--", "TC"), M3 = c("AT", "TT", "AG"))

我想将变量 M1 的字符拆分为 M3 (在真实数据集中,我有 > 6000 个变量)

  name  M1a M1b   M2a M2b  M3a  M3b 
   L1   A    C    C    C    A     T
   L2   A    T    -    -    T     T
   L3   NA   NA   T     C    A     G

我尝试了以下代码:

func<- function(x) {sapply( strsplit(x, ""),
                     match, table= c("A","C","T","G", "--", NA))}

odataframe <- data.frame(apply(mydf, 1, func) )
colnames(odataframe) <-  paste(rep(names(mydf), each = 2), c("a", "b"), sep = "")
odataframe

I want to split characters. Although I have a large dataframe to work, the following small example to show what need to be done.

  mydf <- data.frame (name = c("L1", "L2", "L3"), 
    M1 = c("AC", "AT", NA), M2 = c("CC", "--", "TC"), M3 = c("AT", "TT", "AG"))

I want to split the characters for variables M1 to M3 (in real dataset I have > 6000 variables)

  name  M1a M1b   M2a M2b  M3a  M3b 
   L1   A    C    C    C    A     T
   L2   A    T    -    -    T     T
   L3   NA   NA   T     C    A     G

I tried the following codes:

func<- function(x) {sapply( strsplit(x, ""),
                     match, table= c("A","C","T","G", "--", NA))}

odataframe <- data.frame(apply(mydf, 1, func) )
colnames(odataframe) <-  paste(rep(names(mydf), each = 2), c("a", "b"), sep = "")
odataframe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ペ泪落弦音 2024-12-20 07:33:13

干得好:

splitCol <- function(x){
  x <- as.character(x)
  x[is.na(x)] <- "$"
  z <- matrix(unlist(strsplit(x, split="")), ncol=2, byrow=TRUE)
  z[z=="$"] <- NA
  z
}


newdf <- as.data.frame(do.call(cbind, lapply(mydf[, -1], splitCol)))
names(newdf) <- paste(rep(names(mydf[, -1]), each=2), c("a", "b"), sep="")
newdf <- data.frame(mydf[, 1, drop=FALSE], newdf)

newdf
  name  M1a  M1b M2a M2b M3a M3b
1   L1    A    C   C   C   A   T
2   L2    A    T   -   -   T   T
3   L3 <NA>  <NA   T   C   A   G

Here you go:

splitCol <- function(x){
  x <- as.character(x)
  x[is.na(x)] <- "$"
  z <- matrix(unlist(strsplit(x, split="")), ncol=2, byrow=TRUE)
  z[z=="$"] <- NA
  z
}


newdf <- as.data.frame(do.call(cbind, lapply(mydf[, -1], splitCol)))
names(newdf) <- paste(rep(names(mydf[, -1]), each=2), c("a", "b"), sep="")
newdf <- data.frame(mydf[, 1, drop=FALSE], newdf)

newdf
  name  M1a  M1b M2a M2b M3a M3b
1   L1    A    C   C   C   A   T
2   L2    A    T   -   -   T   T
3   L3 <NA>  <NA   T   C   A   G
小傻瓜 2024-12-20 07:33:13

Andrie 的代码作为可复制函数

splitCol <- function(dataframe, splitVars=names(dataframe)){
split.DF <- dataframe[,splitVars]
keep.DF <- dataframe[, !names(dataframe) %in% c(splitVars)]

X <- function(x)matrix(unlist(strsplit(as.character(x), split="")), ncol=2, byrow=TRUE)

newdf <- as.data.frame(do.call(cbind, suppressWarnings(lapply(split.DF, X))) )
names(newdf) <- paste(rep(names(split.DF), each=2), c(".a", ".b"), sep="") 
data.frame(keep.DF,newdf)
}

测试一下

splitCol(mydf)
splitCol(mydf, c('M1','M2'))

请不要将此选为正确答案。安德烈的答案显然是第一个正确答案。这只是他的代码扩展到更多情况。感谢您的提问,也感谢安德烈提供的代码。

Andrie's code as a replicable function

splitCol <- function(dataframe, splitVars=names(dataframe)){
split.DF <- dataframe[,splitVars]
keep.DF <- dataframe[, !names(dataframe) %in% c(splitVars)]

X <- function(x)matrix(unlist(strsplit(as.character(x), split="")), ncol=2, byrow=TRUE)

newdf <- as.data.frame(do.call(cbind, suppressWarnings(lapply(split.DF, X))) )
names(newdf) <- paste(rep(names(split.DF), each=2), c(".a", ".b"), sep="") 
data.frame(keep.DF,newdf)
}

Test it out

splitCol(mydf)
splitCol(mydf, c('M1','M2'))

Please don't vote this as the correct answer. Andrie's answer is clearly the first correct answer. This is just an extension of his code to more situations. Thanks for the question and thanks for the code Andrie.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文