使用收集时尝试维护索引

如何解决使用收集时尝试维护索引

我正在尝试将我的数据从宽转换为长,但是由于某种原因,ID列没有 转换后显示。这是我的数据:

> head(dca)

# A tibble: 6 x 11
  ResponseId  Q9        Q10       Q11       Q12       Q13       Q14       Q15      Q16      Q17      Q18     
  <chr>       <chr>     <chr>     <chr>     <chr>     <chr>     <chr>     <chr>    <chr>    <chr>    <chr>   
1 "Response … "Regardi… "Regardi… "Regardi… "Regardi… "Regardi… "Regardi… "Regard… "Regard… "Regard… "Regard…
2 "{\"Import… "{\"Impo… "{\"Impo… "{\"Impo… "{\"Impo… "{\"Impo… "{\"Impo… "{\"Imp… "{\"Imp… "{\"Imp… "{\"Imp…
3 "R_2V7lrA7… "Using F… "Using T… "Using Y… "Using T… "Using T… "Using I… "Using … "Using … "Using … "Using …
4 "R_3nozPOT… "Using F… "Using T… "Using T… "Using T… "Using T… "Using I… "Using … "Using … "Using … "Using …
5 "R_2TB0Wwy… "Using Y… "Using T… "Using T… "Using T… "Using T… "Using F… "Using … "Using … "Using … "Using …
6 "R_2woFtS9… "Using Y… "Using T… "Using Y… "Using T… "Using I… "Using I… "Using … "Using … "Using … "Using …

应用以下转换后:

library(tidyr)
keycol <- "ResponseId"
valuecol <- "Response"
gathercols <- c("Q9","Q10","Q11","Q12","Q13","Q14","Q15","Q16","Q17","Q18" )
dca_long<- gather_(dca,dca$ResponseId,keycol,valuecol,gathercols)

这就是我得到的:

> head(dca_long)
# A tibble: 6 x 2
  ResponseId 'Response`                                                               
  <chr>      <chr>                                                                                
1 Q9         "Regarding the use of social media,which of the following options would you prefer?"
2 Q9         "{\"ImportId\":\"QID12\"}"                                                           
3 Q9         "Using Facebook on PC for utility"                                                   
4 Q9         "Using Facebook on PC for utility"                                                   
5 Q9         "Using Youtube on mobile for entertainment"                                          
6 Q9         "Using Youtube on mobile for entertainment"      

基本上,我希望dca_long中有一列具有与dca中ResponseId的值匹配的列。我这样做是为了使dca进一步适合mlogit()

评论中有人要求输出以更好地理解代码:

> dput(head(dca))
structure(list(ResponseId = c("Response ID","{\"ImportId\":\"_recordId\"}","R_2V7lrA7n29xU0i6","R_3nozPOTbJBE1OBa","R_2TB0WwyWCugTyEg","R_2woFtS93jHyiv8F"),Q9 = c("Regarding the use of social media,which of the following options would you prefer?","{\"ImportId\":\"QID12\"}","Using Facebook on PC for utility","Using Youtube on mobile for entertainment","Using Youtube on mobile for entertainment"),Q10 = c("Regarding the use of social media,"{\"ImportId\":\"QID13\"}","Using TikTok on PC for utility","Using Twitter on mobile for entertainment","Using Twitter on mobile for entertainment"),Q11 = c("Regarding the use of social media,"{\"ImportId\":\"QID14\"}","Using Youtube on PC for utility","Using Youtube on PC for utility"),Q12 = c("Regarding the use of social media,"{\"ImportId\":\"QID15\"}","Using Twitter on mobile for utility","Using Twitter on mobile for utility"),Q13 = c("Regarding the use of social media,"{\"ImportId\":\"QID16\"}","Using TikTok on Mobile for utility","Using Instagram on PC for entertainment"),Q14 = c("Regarding the use of social media,"{\"ImportId\":\"QID17\"}","Using Instagram on PC for utility","Using Facebook on Mobile for entertainment","Using Instagram on PC for utility"),Q15 = c("Regarding the use of social media,"{\"ImportId\":\"QID18\"}","Using Facebook on mobile for utility"),Q16 = c("Regarding the use of social media,"{\"ImportId\":\"QID19\"}","Using Facebook on PC for entertainment","Using Tiktok on mobile for utility","Using Facebook on PC for entertainment"),Q17 = c("Regarding the use of social media,"{\"ImportId\":\"QID20\"}","Using Youtube on PC for entertainment","Using Instagram on mobile for utility","Using Instagram on mobile for utility"),Q18 = c("Regarding the use of social media,"{\"ImportId\":\"QID21\"}","Using Instagram on mobile for entertainment","Using Instagram on mobile for entertainment")),row.names = c(NA,-6L),class = c("tbl_df","tbl","data.frame"))

解决方法

此示例可以帮助您解决问题。

#simulated wide data for reproducibility
wide_data <- read.table(header=TRUE,text='
 subject sex time_1  time_2  time_3   
       1   M     15   16    23 
       2   F     25   20    48 
       3   F     30   25    55 
       4   M     35   32    60 
')

使用gather,您应该得到类似以下的内容。

gather(data = olddata_wide,key = alternative,value = time,c(time_1,time_2,time_3),factor_key=TRUE) 


   subject sex alternative time
1        1   M      time_1   15
2        2   F      time_1   25
3        3   F      time_1   30
4        4   M      time_1   35
5        1   M      time_2   16
6        2   F      time_2   20
7        3   F      time_2   25
8        4   M      time_2   32
9        1   M      time_3   23
10       2   F      time_3   48
11       3   F      time_3   55
12       4   M      time_3   60

如果这没有帮助。请复制一小段数据(dca)来处理其中的数据。最好!

[已编辑]

使用您发布的数据:

df<- structure(list(ResponseId = c("Response ID","{\"ImportId\":\"_recordId\"}","R_2V7lrA7n29xU0i6","R_3nozPOTbJBE1OBa","R_2TB0WwyWCugTyEg","R_2woFtS93jHyiv8F"),Q9 = c("Regarding the use of social media,which of the following options would you prefer?","{\"ImportId\":\"QID12\"}","Using Facebook on PC for utility","Using Youtube on mobile for entertainment","Using Youtube on mobile for entertainment"),Q10 = c("Regarding the use of social media,"{\"ImportId\":\"QID13\"}","Using TikTok on PC for utility","Using Twitter on mobile for entertainment","Using Twitter on mobile for entertainment"),Q11 = c("Regarding the use of social media,"{\"ImportId\":\"QID14\"}","Using Youtube on PC for utility","Using Youtube on PC for utility"),Q12 = c("Regarding the use of social media,"{\"ImportId\":\"QID15\"}","Using Twitter on mobile for utility","Using Twitter on mobile for utility"),Q13 = c("Regarding the use of social media,"{\"ImportId\":\"QID16\"}","Using TikTok on Mobile for utility","Using Instagram on PC for entertainment"),Q14 = c("Regarding the use of social media,"{\"ImportId\":\"QID17\"}","Using Instagram on PC for utility","Using Facebook on Mobile for entertainment","Using Instagram on PC for utility"),Q15 = c("Regarding the use of social media,"{\"ImportId\":\"QID18\"}","Using Facebook on mobile for utility"),Q16 = c("Regarding the use of social media,"{\"ImportId\":\"QID19\"}","Using Facebook on PC for entertainment","Using Tiktok on mobile for utility","Using Facebook on PC for entertainment"),Q17 = c("Regarding the use of social media,"{\"ImportId\":\"QID20\"}","Using Youtube on PC for entertainment","Using Instagram on mobile for utility","Using Instagram on mobile for utility"),Q18 = c("Regarding the use of social media,"{\"ImportId\":\"QID21\"}","Using Instagram on mobile for entertainment","Using Instagram on mobile for entertainment")),row.names = c(NA,-6L),class = c("tbl_df","tbl","data.frame"))

并使用我先前发布的摘录部分:

df_long<- gather(data = df,value = value_answer,Q9:Q18,factor_key=TRUE) 

您应该能够获得如下内容,其中保留了Response ID变量:

  ResponseId             alternative value_answer                                          
  <chr>                  <fct>       <chr>                                                 
1 "Response ID"          Q9          "Regarding the use of social media,which of the foll~
2 "{\"ImportId\":\"_rec~ Q9          "{\"ImportId\":\"QID12\"}"                            
3 "R_2V7lrA7n29xU0i6"    Q9          "Using Facebook on PC for utility"                    
4 "R_3nozPOTbJBE1OBa"    Q9          "Using Facebook on PC for utility"                    
5 "R_2TB0WwyWCugTyEg"    Q9          "Using Youtube on mobile for entertainment"           
6 "R_2woFtS93jHyiv8F"    Q9          "Using Youtube on mobile for entertainment"     

我希望这会对您有所帮助。最好!

,

这接近您要完成的工作吗?

library(tidyr)
library(dplyr)

dca_long <- dca %>% 
  gather(key = "question",value = "response",2:ncol(.))

# # A tibble: 60 x 3
# ResponseId                     question response                                                                             
# <chr>                          <chr>    <chr>                                                                                
#  1 "Response ID"                  Q9       "Regarding the use of social media,which of the following options would you prefer?"
#  2 "{\"ImportId\":\"_recordId\"}" Q9       "{\"ImportId\":\"QID12\"}"                                                           
#  3 "R_2V7lrA7n29xU0i6"            Q9       "Using Facebook on PC for utility"                                                   
#  4 "R_3nozPOTbJBE1OBa"            Q9       "Using Facebook on PC for utility"                                                   
#  5 "R_2TB0WwyWCugTyEg"            Q9       "Using Youtube on mobile for entertainment"                                          
#  6 "R_2woFtS93jHyiv8F"            Q9       "Using Youtube on mobile for entertainment"                                          
#  7 "Response ID"                  Q10      "Regarding the use of social media,which of the following options would you prefer?"
#  8 "{\"ImportId\":\"_recordId\"}" Q10      "{\"ImportId\":\"QID13\"}"                                                           
#  9 "R_2V7lrA7n29xU0i6"            Q10      "Using TikTok on PC for utility"                                                     
# 10 "R_3nozPOTbJBE1OBa"            Q10      "Using Twitter on mobile for entertainment"                                          
# # … with 50 more rows

看起来您的数据仍然需要清理。

本质上,我希望dca_long中有一列,以使dca中的ResponseId的值匹配。

all(unique(dca$ResponseId) == unique(dca_long$ResponseId))

# [1] TRUE

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-