将7个不同的列拆分为R中的5个不同范围

时间:2016-03-11 07:04:00

标签: r

我想知道如何将七个不同的列分成5个不同的范围/级别。每个级别都有一定的范围。以下是数据。

Name    V1.1    V1.2    V2.1    V2.2    V3.1    V3.2
Arn     Eng     80      Sci      50     Mat      17
Brn     Sci     90      Eng      60     Mat      28
Crn     Mat     100     Sci      47     Eng      27.

五个铲斗范围如下。 < = 20 Level1,< = 40 Level2,< = 60 Level3,< = 80 Level4,Level5

预期产出:

Name Level1   Level2   Level3   Level4  Level5
Arn  Mat      NA        Sci      Eng     NA
Brn  NA       Mat       Eng      NA      Sci
Crn  NA       Eng       Sci      NA      Mat    

1 个答案:

答案 0 :(得分:0)

我们可以使用cut根据数据集中的数字列创建不同的“级别”。我们使用lapply遍历数字列,使用cut,其中breaks与OP的帖子一样,labels用于指定“级别”。将'data.frame'转换为'data.table'(setDT(df2)),melt转换为'long'格式,然后将dcast转换为'wide'格式。

df2 <- df1
df2[c(3,5,7)] <- lapply(df1[c(3,5,7)], function(x) 
     cut(x, breaks=c(-Inf, 20, 40, 60, 80, Inf),
     labels= paste0("Level", 1:5)))
library(data.table)
dM <- melt(setDT(df2), measure = list(seq(2,ncol(df2),
                   by =2), seq(3, ncol(df2), by = 2)))
dcast(dM, Name~value2, value.var='value1')
#   Name Level1 Level2 Level3 Level4 Level5
#1:  Arn    Mat     NA    Sci    Eng     NA
#2:  Brn     NA    Mat    Eng     NA    Sci
#3:  Crn     NA    Eng    Sci     NA    Mat

这是OP的预期输出

# Name Level1   Level2   Level3   Level4  Level5
# Arn  Mat      NA        Sci      Eng     NA
# Brn  NA       Mat       Eng      NA      Sci
# Crn  NA       Eng       Sci      NA      Mat

数据

df1 <- structure(list(Name = c("Arn", "Brn", "Crn"),
V1.1 = c("Eng", 
"Sci", "Mat"), V1.2 = c(80L, 90L, 100L), V2.1 = c("Sci", "Eng", 
"Sci"), V2.2 = c(50L, 60L, 47L), V3.1 = c("Mat", "Mat", "Eng"
 ), V3.2 = c(17L, 28L, 27L)), .Names = c("Name", "V1.1", "V1.2", 
"V2.1", "V2.2", "V3.1", "V3.2"), class = "data.frame",
row.names = c(NA, -3L))