Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/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 - Fatal编程技术网

R 在数据帧中重新组合分类值

R 在数据帧中重新组合分类值,r,R,我在寻找一个建议:我正试图通过一个变量值对数据帧重新排序/分组 例如,转换本机数据帧变量 变成这样: 到目前为止,我已经尝试使用cbind/rbind进行循环,具体取决于数据的组织、聚合、应用等方式。但总有一些问题阻碍了方法的工作 谢谢你的帮助 首先,我想指出,阅读如何给出一个有用的示例,以及使用dput的原始数据将大大有助于获得反馈。也就是说: For the dataset you showed: A <- structure(list(Var_Typer = c("cnt"

我在寻找一个建议:我正试图通过一个变量值对数据帧重新排序/分组

例如,转换本机数据帧变量

变成这样:

到目前为止,我已经尝试使用cbind/rbind进行循环
,具体取决于数据的组织、聚合、应用等方式。但总有一些问题阻碍了方法的工作


谢谢你的帮助

首先,我想指出,阅读如何给出一个有用的示例,以及使用
dput
的原始数据将大大有助于获得反馈。也就是说:

For the dataset you showed:


 A <- structure(list(Var_Typer = c("cnt", "Cont", "cnt", "cnt", "fact", 
"fact", "Char", "Char", "Cont"), R_FIELD = c("Gender", "Age", 
"Activation", "WakeUpStroke", "ArMode", "PreHospActiv", "EMTag", 
"EMTdx", "EMTlams")), .Names = c("Var_Typer", "R_FIELD"), row.names = c(NA, 
-9L), class = "data.frame")

    > head(A)
  Var_Typer      R_FIELD
1       cnt       Gender
2      Cont          Age
3       cnt   Activation
4       cnt WakeUpStroke
5      fact       ArMode
6      fact PreHospActiv



 B <- apply(
       dcast(A, Var_Typer ~ R_FIELD, value.var = 'R_FIELD'), 1, function(i){
         ndf <- as.data.frame(rbind(i[complete.cases(i)]))
         colnames(ndf) <- c('Class',1:(length(ndf)-1))
         ndf
       }) %>% rbind.pages %>% (function(x){
              x[is.na(x)] <- "..."
              x
            })


   Class          1            2            3
 1  Char      EMTag        EMTdx          ...
 2   cnt Activation       Gender WakeUpStroke
 3  Cont        Age      EMTlams          ...
 4  fact     ArMode PreHospActiv          ...
对于您显示的数据集:
A头(A)
Var_打字机R_字段
1 cnt性别
2续
3碳纳米管活化
4 cnt上行程
5事实装甲
6事实院前活动

B为了简洁起见@docween我想指出,这是一种非常无用的数据结构方式。