简体   繁体   English

将列级别设置为新列 R

[英]Make column levels into new columns R

I have a data table[1000+, 4] .我有一个data table[1000+, 4]

One of the columns has 5 levels and I want to make these 5 levels into 5 new columns.其中一列有5 levels ,我想将这 5 个级别变成 5 个新列。

Any suggestions?有什么建议?

We extract the levels from the column of interest ('ColN'), convert to list and assign that to five 'new' columns.我们从感兴趣的列 ('ColN') 中提取levels ,转换为list并将其分配给五个“新”列。

df1[paste0("new", 1:5)] <- as.list(levels(df1$ColN))
head(df1,2)
#    ColN Col2 Col3 new1 new2 new3 new4 new5
#1    C    7    a    A    B    C    D    E
#2    A    4    b    A    B    C    D    E
levels(df1$ColN)
#[1] "A" "B" "C" "D" "E"
data 数据
set.seed(48) df1 <- data.frame(ColN= sample(LETTERS[1:5], 10, replace=TRUE), Col2= sample(10), Col3= letters[1:10])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 R 的新变量列中创建新的分类变量级别 - Make new Levels of categorical variable in new Variable Column in R 将一列(长)的级别重塑为新列(宽) - Reshape levels of a column (long) into new columns (wide) R:根据其他两列的标准,创建一个具有多个类别(级别)的新列 - R: Create a new column with multiple categories(levels) based on the critera of two other columns 如何使用R中的plyr包创建与给定列级别相对应的新数据框列? - How can I create new data frame columns corresponding to levels of a given column using the plyr package in R? 按名称将所有因子级别返回为三列data.table [R]中的新列 - Return all factor levels by name as new columns from a three column data.table [R] 如何将列中的分类数据分成R中的新不同列? - How to make categorical data in a column into new distinct columns in R? 根据其他列创建一个包含值的新列 - 在 R 中 - Make a new column with values according to other columns - in R 根据R中的因子水平进行汇总(是否创建新列?) - Aggregating according to factor levels in R (create new columns?) R-根据频率表变量的级别创建新列 - R- create new columns based on levels of a freq table variable 将不同列中的相同因子水平移到R中的同一列中 - Move same factor levels in different columns into same column in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM