拆分列中的所有值以创建新列

问题描述 投票:0回答:2

我有一个整数数据帧,我想将其转换为位,然后将每个结果位序列分成不同的列。我想我可以将我的位转换为字符串,然后将字符串拆分为空格。但是,我的方法不起作用。

我创建了我的数据框:

r1 <- c(24,25,27)
r2 <- c(98,102,4)
model.data <- as.data.frame(rbind(r1,r2))

然后我将int转换为位:

model.data[] <- lapply(model.data, function(x) {
 if(!is.na(x)) as.character(paste(intToBits(x), collapse = " ")) else x
})

但是,这是我遇到第一个问题的地方。我的数据框的第二行现在似乎与第一行具有相同的值:

model.data $ V1

[1] "00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"

[2] "00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"

情况并非如此,因为原始数据框中的值不同。这里的所有列都是相同的情况(V2,V3)。

我的第二个问题是我无法拆分列。我尝试这种方法:

for(i in names(model.data)) {
  a.split <- strsplit(model.data[[i]], split = " ")
  cbind(model.data,a.split)
}

但它什么都没做。

你能帮我搞清楚我做错了什么吗?谢谢!

r
2个回答
2
投票

你需要使用apply对行和列进行ifelse。有趣的是,使用lapply似乎不起作用。这里是apply解决方案的一个例子:

r1 <- c(24,25,27)
r2 <- c(98,102,4)
model.data <- as.data.frame(rbind(r1,r2))

model.data[] <- as.data.frame(apply(model.data, c(1,2), function(x) ifelse(!is.na(x), as.character(paste(intToBits(x), collapse = " ")), x)), stringsAsFactors = FALSE)

> model.data
                                                                                                V1
r1 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
                                                                                                V2
r1 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 01 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
                                                                                                V3
r1 01 01 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

编辑:

要创建每个变量一位的新数据,首先lapply strsplit,给你一个嵌套列表作为输出,然后使用嵌套的do.call首先cbind子列表,然后rbind结果:

newCols <- lapply(model.data, function(x) strsplit(x, split = " "))

a.split <- as.data.frame(do.call(rbind, do.call(cbind, newCols)), stringsAsFactors = FALSE)

> a.split
  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32
1 00 00 00 01 01 00 00 00 00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00
2 00 01 00 00 00 01 01 00 00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00
3 01 00 00 01 01 00 00 00 00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00
4 00 01 01 00 00 01 01 00 00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00
5 01 01 00 01 01 00 00 00 00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00
6 00 00 01 00 00 00 00 00 00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00

3
投票

使用tidyr包中的“独立”功能

    library(tidyr)

    # Your data
    r1 <- c(24,25,27)
    r2 <- c(98,102,4)
    # Concatenate r1 and r2
    r3 <- c(r1,r2)
    # Create a dataframe
    model.data <- as.data.frame(r3)
    # Check type of r3
    class(model.data$r3)
    # Transform with "intToBits" function
    model.data[] <- lapply(model.data, function(x) {
     if(!is.na(x)) as.character(paste(intToBits(x), collapse = " ")) else x
    })

    nb = length(unlist(strsplit(model.data$r3[1]," ")))
    # Transform into character
    model.data$r3 <- as.character(model.data$r3)
    # Use "separate" function from tidyr, generating nb variable 
    model.data %>% separate(r3, into = paste0("V",seq(1, nb, by = 1)), sep = " ")
© www.soinside.com 2019 - 2024. All rights reserved.