将一列拆分为多个二进制虚拟列

编程入门 行业动态 更新时间:2024-10-25 06:27:27
本文介绍了将一列拆分为多个二进制虚拟列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图将数据框中的单个字符"变量拆分为多个因子"变量.

I'm trying to split a single "character" variable in my dataframe into mutiple "factor" variables.

> sampledf=data.frame(vin=c('v1','v2','v3'),features=c('f1:f2:f3','f2:f4:f5','f1:f4:f5')) > sampledf vin features 1 v1 f1:f2:f3 2 v2 f2:f4:f5 3 v3 f1:f4:f5 > desireddf=data.frame(vin=c('v1','v2','v3'),f1=c(1,0,1),f2=c(1,1,0),f3=c(1,0,0),f4=c(0,1,1),f5=c(0,1,1)) > desireddf vin f1 f2 f3 f4 f5 1 v1 1 1 1 0 0 2 v2 0 1 0 1 1 3 v3 1 0 0 1 1

我尝试使用 strsplit() 来分隔功能"列

I've tried using strsplit() to separate the "features" column

strsplit(as.character(df$features), ";")

但没有运气分解它们.

推荐答案

我们可以使用 mtabulate 从 qdapTools 拆分后(strsplit(..>)功能"列.

We can use mtabulate from qdapTools after splitting (strsplit(..) the 'features' column.

library(qdapTools) cbind(sampledf[1],mtabulate(strsplit(as.character(sampledf$features), ':'))) # vin f1 f2 f3 f4 f5 #1 v1 1 1 1 0 0 #2 v2 0 1 0 1 1 #3 v3 1 0 0 1 1

或者我们可以使用 library(splitstackshape)

library(splitstackshape) df1 <- cSplit_e(sampledf, 'features', ':', type= 'character', fill=0, drop=TRUE) names(df1) <- sub('.*_', '', names(df1))

或者使用base R方法,我们像以前一样split,从strsplit 与 'vin' 列,使用 stack 转换为键/值列 'data.frame',获取 table,转置和 cbind 'sampledf' 的第一列.

Or using base R methods, we split as before, set the names of the list elements from the strsplit with 'vin' column, convert to a key/value columns 'data.frame' using stack, get the table, transpose and cbind with the first column of 'sampledf'.

cbind(sampledf[1], t(table(stack(setNames(strsplit(as.character(sampledf$features), ':'), sampledf$vin)))))

更多推荐

将一列拆分为多个二进制虚拟列

本文发布于:2023-10-26 12:06:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530090.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!