我正在尝试将列名字符串拆分为单独的列,但我面临的问题是行具有逻辑值。有一些帖子在行中使用字符串拆分列,但我找不到任何具有逻辑值的帖子。
我的data.frame看起来像这样:
mydf <- data.frame (author = c("N1", "N2", "N3"),
Aa..Ab = c(T, T, F),
BB = c(T, F, T),
Ca...Cb = c(F, F, T))
结果看起来应该是这样的
mydfnew <- data.frame (author = c("N1", "N2", "N3"),
Aa = c(T, T, F),
Ab = c(T, T, F),
BB = c(T, F, T),
Ca = c(F, F, T),
Cb = c(F, F, T))
我尝试调整分割列和名称(Split character in column and name)的代码,如下所示:
splitCol <- function(dataframe, splitVars=names(dataframe)){
split.DF <- dataframe[,splitVars]
keep.DF <- dataframe[, !names(dataframe) %in% c(splitVars)]
X <- function(x)matrix(unlist(rep(x)), 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)
我收到错误:
名称错误(newdf)&lt; - paste(rep(names(split.DF),每个= 2),c(“。a”,: 'names'属性[8]的长度必须与vector [4]
的长度相同
答案 0 :(得分:2)
以下是使用replicate
和Map
as.data.frame(Map(x = strsplit(names(mydf), '[.]+'),
DATA = mydf,
f = function(x,DATA){
setNames(replicate(length(x), DATA, simplify = FALSE),x )}
))
## author Aa Ab BB Ca Cb
## 1 N1 TRUE TRUE TRUE FALSE FALSE
## 2 N2 TRUE TRUE FALSE FALSE FALSE
## 3 N3 FALSE FALSE TRUE TRUE TRUE
答案 1 :(得分:0)
答案基本上是@ mnel版本的两步变体:
splnames <- strsplit(names(mydf),"\\.+")
setNames(data.frame(rep(mydf,sapply(splnames,length))),unlist(splnames))
结果:
author Aa Ab BB Ca Cb
1 N1 TRUE TRUE TRUE FALSE FALSE
2 N2 TRUE TRUE FALSE FALSE FALSE
3 N3 FALSE FALSE TRUE TRUE TRUE
答案 2 :(得分:0)
这是使用“reshape2”包和我的“splitstackshape”包的替代方法。基本思想是获得一个比较宽的数据集更容易操作的“长”数据集 - 即使最终目标是创建一个宽数据集:)
## Load required packages
library(reshape2)
library(splitstackshape)
## Create a "long" version of your dataset
A <- melt(mydf, id.vars="author")
B <- concat.split.multiple(A, "variable", ".", "long")
## Use `dcast` to get the desired wide form
## Use `na.omit(B)` to get rid of the rows that have
## NA before doing the transformation
dcast(na.omit(B), author ~ variable, value.var="value")
# author Aa BB Ca Ab Cb
# 1 N1 TRUE TRUE FALSE TRUE FALSE
# 2 N2 TRUE FALSE FALSE TRUE FALSE
# 3 N3 FALSE TRUE TRUE FALSE TRUE