Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 融化数据,保持某些列成对_R_Reshape_Melt - Fatal编程技术网

R 融化数据,保持某些列成对

R 融化数据,保持某些列成对,r,reshape,melt,R,Reshape,Melt,我有以下资料: DT <- structure(list(ECOST = c("Choice_01", "Choice_02", "Choice_03", "Choice_04", "Choice_05", "Choice_06", "Choice_07", "Choice_08", "Choice_09",

我有以下资料:

DT <- structure(list(ECOST = c("Choice_01", "Choice_02", "Choice_03", 
"Choice_04", "Choice_05", "Choice_06", "Choice_07", "Choice_08", 
"Choice_09", "Choice_10", "Choice_11", "Choice_12"), control = c(18, 
30, 47, 66, 86, 35, 31, 46, 55, 39, 55, 41), treatment = c(31, 
35, 46, 68, 86, 36, 32, 42, 52, 39, 58, 43), control_p = c(0.163636363636364, 
0.272727272727273, 0.427272727272727, 0.6, 0.781818181818182, 
0.318181818181818, 0.281818181818182, 0.418181818181818, 0.5, 
0.354545454545455, 0.5, 0.372727272727273), treatment_p = c(0.319587628865979, 
0.360824742268041, 0.474226804123711, 0.701030927835051, 0.88659793814433, 
0.371134020618557, 0.329896907216495, 0.43298969072165, 0.536082474226804, 
0.402061855670103, 0.597938144329897, 0.443298969072165)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

# A tibble: 12 x 5
   ECOST     control treatment control_p treatment_p
   <chr>       <dbl>     <dbl>     <dbl>       <dbl>
 1 Choice_01      18        31     0.164       0.320
 2 Choice_02      30        35     0.273       0.361
 3 Choice_03      47        46     0.427       0.474
 4 Choice_04      66        68     0.6         0.701
 5 Choice_05      86        86     0.782       0.887
 6 Choice_06      35        36     0.318       0.371
 7 Choice_07      31        32     0.282       0.330
 8 Choice_08      46        42     0.418       0.433
 9 Choice_09      55        52     0.5         0.536
10 Choice_10      39        39     0.355       0.402
11 Choice_11      55        58     0.5         0.598
12 Choice_12      41        43     0.373       0.443

使用
pivot\u longer
、一些数据争用以及之后的
pivot\u wide
您可以实现所需的结果,如下所示:

library(tidyr)
图书馆(dplyr)
DT%>%
枢轴长度(-ECOST)%>%
分离(名称,分为=c(“组”,“什么”))%>%
突变(what=ifelse(is.na(what),“count”,“percentage”))%>%
枢轴(名称来自“什么”,值来自“值”)
#>#tibble:24 x 4
#>生态成本组计数百分比
#>                    
#>1选择_01控制18 0.164
#>2选择_01治疗31 0.320
#>3选择_02控制30 0.273
#>4选择02治疗35 0.361
#>5选项03控制47 0.427
#>6选择_03治疗46 0.474
#>7选择04控制66 0.6
#>8选择04治疗68 0.701
#>9选择_05对照86 0.782
#>10选择_05治疗86 0.887
#>#…还有14行

由(v1.0.0)于2021年2月21日创建,使用
pivot\u longer
,一些数据争用,然后
pivot\u更宽
您可以实现如下所示的预期结果:

library(tidyr)
图书馆(dplyr)
DT%>%
枢轴长度(-ECOST)%>%
分离(名称,分为=c(“组”,“什么”))%>%
突变(what=ifelse(is.na(what),“count”,“percentage”))%>%
枢轴(名称来自“什么”,值来自“值”)
#>#tibble:24 x 4
#>生态成本组计数百分比
#>                    
#>1选择_01控制18 0.164
#>2选择_01治疗31 0.320
#>3选择_02控制30 0.273
#>4选择02治疗35 0.361
#>5选项03控制47 0.427
#>6选择_03治疗46 0.474
#>7选择04控制66 0.6
#>8选择04治疗68 0.701
#>9选择_05对照86 0.782
#>10选择_05治疗86 0.887
#>#…还有14行

由(v1.0.0)于2021-02-21创建,您可以重命名列,以便在
count
percentage
列之间有明确的区别,然后再使用
pivot\u

library(dplyr)
library(tidyr)


DT %>%
  rename_with(~paste(sub('_.*', '', .), 
              rep(c('count', 'percentage'), each = 2), sep = '_'), -1) %>%
  pivot_longer(cols = -ECOST, 
               names_to = c('group', '.value'), 
               names_sep = '_')

# A tibble: 24 x 4
#   ECOST     group     count percentage
#   <chr>     <chr>     <dbl>      <dbl>
# 1 Choice_01 control      18      0.164
# 2 Choice_01 treatment    31      0.320
# 3 Choice_02 control      30      0.273
# 4 Choice_02 treatment    35      0.361
# 5 Choice_03 control      47      0.427
# 6 Choice_03 treatment    46      0.474
# 7 Choice_04 control      66      0.6  
# 8 Choice_04 treatment    68      0.701
# 9 Choice_05 control      86      0.782
#10 Choice_05 treatment    86      0.887
# … with 14 more rows
库(dplyr)
图书馆(tidyr)
DT%>%
将_重命名为(~paste(sub)(“.*”,“,”),
代表(c('count','percentage'),各=2),sep='''.',-1)%>%
枢轴长度(cols=-ECOST,
name_to=c('group','.value'),
名称_sep='')
#A tibble:24 x 4
#生态成本组计数百分比
#                   
#1选择_01控制18 0.164
#2选择_01治疗31 0.320
#3选择_02控制30 0.273
#4选择02治疗35 0.361
#5选项03控制47 0.427
#6选择_03治疗46 0.474
#7选择04控制66 0.6
#8选择04治疗68 0.701
#9选择_05对照86 0.782
#10选择_05治疗86 0.887
#…还有14行

您可以重命名列,以便清楚区分
计数
百分比
列,然后再使用
pivot\u

library(dplyr)
library(tidyr)


DT %>%
  rename_with(~paste(sub('_.*', '', .), 
              rep(c('count', 'percentage'), each = 2), sep = '_'), -1) %>%
  pivot_longer(cols = -ECOST, 
               names_to = c('group', '.value'), 
               names_sep = '_')

# A tibble: 24 x 4
#   ECOST     group     count percentage
#   <chr>     <chr>     <dbl>      <dbl>
# 1 Choice_01 control      18      0.164
# 2 Choice_01 treatment    31      0.320
# 3 Choice_02 control      30      0.273
# 4 Choice_02 treatment    35      0.361
# 5 Choice_03 control      47      0.427
# 6 Choice_03 treatment    46      0.474
# 7 Choice_04 control      66      0.6  
# 8 Choice_04 treatment    68      0.701
# 9 Choice_05 control      86      0.782
#10 Choice_05 treatment    86      0.887
# … with 14 more rows
库(dplyr)
图书馆(tidyr)
DT%>%
将_重命名为(~paste(sub)(“.*”,“,”),
代表(c('count','percentage'),各=2),sep='''.',-1)%>%
枢轴长度(cols=-ECOST,
name_to=c('group','.value'),
名称_sep='')
#A tibble:24 x 4
#生态成本组计数百分比
#                   
#1选择_01控制18 0.164
#2选择_01治疗31 0.320
#3选择_02控制30 0.273
#4选择02治疗35 0.361
#5选项03控制47 0.427
#6选择_03治疗46 0.474
#7选择04控制66 0.6
#8选择04治疗68 0.701
#9选择_05对照86 0.782
#10选择_05治疗86 0.887
#…还有14行

这里是一个
数据表
方法,其中包含了
melt.data.table()的限制/功能

库(data.table)
setDT(DT)
#获取后缀

后缀这里是一个
data.table
方法,它具有
melt.data.table()

库(data.table)
setDT(DT)
#获取后缀

后缀被接受为最容易理解(至少对我而言),因此(对我而言)最容易适应其他情况。被接受为最容易理解(至少对我而言),因此(对我而言)最容易适应其他情况。
library( data.table )
setDT(DT)
#get suffixes
suffix <- unique( sub( "(^.*)(_[a-z])", "\\1", names( DT[ , -1] ) ) )
#melt
DT2 <- melt( DT, id.vars = "ECOST", measure.vars = patterns( count = "[a-oq-z]$", percentage = "_p$"))
#replace factor-levels with the colnames
setattr(DT2$variable, "levels", suffix )

        ECOST  variable count percentage
 1: Choice_01   control    18  0.1636364
 2: Choice_02   control    30  0.2727273
 3: Choice_03   control    47  0.4272727
 4: Choice_04   control    66  0.6000000
 5: Choice_05   control    86  0.7818182
 6: Choice_06   control    35  0.3181818
 7: Choice_07   control    31  0.2818182
 8: Choice_08   control    46  0.4181818
 9: Choice_09   control    55  0.5000000
10: Choice_10   control    39  0.3545455
11: Choice_11   control    55  0.5000000
12: Choice_12   control    41  0.3727273
13: Choice_01 treatment    31  0.3195876
14: Choice_02 treatment    35  0.3608247
15: Choice_03 treatment    46  0.4742268
16: Choice_04 treatment    68  0.7010309
17: Choice_05 treatment    86  0.8865979
18: Choice_06 treatment    36  0.3711340
19: Choice_07 treatment    32  0.3298969
20: Choice_08 treatment    42  0.4329897
21: Choice_09 treatment    52  0.5360825
22: Choice_10 treatment    39  0.4020619
23: Choice_11 treatment    58  0.5979381
24: Choice_12 treatment    43  0.4432990
        ECOST  variable count percentage