将 .dta 导入 R

如何解决将 .dta 导入 R

我目前正在尝试将数据从 .dta 导入到 R。这些数据也有 .tab 格式。如何将变量作为标签导入?

我使用的代码:

data <- read_dta("file.dta")

data <- read.table("file.tab",header = F,sep = "\t",fill = TRUE)

有没有办法以R识别标签的方式导入变量?

Data before importing to R

Data after importing to R

样本数据

structure(list(sn1 = structure(c(1,1,1),label = " point number  ",format.stata = "%8.0g"),wjob12s5 = structure(c(NA,NA,0),label = " day 5/ twelfth job line starts at  ",format.stata = "%8.0g",labels = c(` 4:01 4:15 ` = 1,` 4:16 4:30 ` = 2,` 4:31 4:45 ` = 3,` 4:46 5:00 ` = 4,` 5:01 5:15 ` = 5,` 5:16 5:30 ` = 6,` 5:31 5:45 ` = 7,` 5:46 6:00 ` = 8,` 6:01 6:15 ` = 9,` 6:16 6:30 ` = 10,` 6:31 6:45 ` = 11,` 6:46 7:00 ` = 12,` 7:01 7:15 ` = 13,` 7:16 7:30 ` = 14,` 7:31 7:45 ` = 15,` 7:46 8:00 ` = 16,` 8:01 8:15 ` = 17,` 8:16 8:30 ` = 18,` 8:31 8:45 ` = 19,` 8:46 9:00 ` = 20,` 9:01 9:15 ` = 21,` 9:16 9:30 ` = 22,` 9:31 9:45 ` = 23,` 9:46 10:00 ` = 24,` 10:01 10:15 ` = 25,` 10:16 10:30 ` = 26,` 10:31 10:45 ` = 27,` 10:46 11:00 ` = 28,` 11:01 11:15 ` = 29,` 11:16 11:30 ` = 30,` 11:31 11:45 ` = 31,` 11:46 12:00 ` = 32,` 12:01 12:15 ` = 33,` 12:16 12:30 ` = 34,` 12:31 12:45 ` = 35,` 12:46 13:00 ` = 36,` 13:01 13:15 ` = 37,` 13:16 13:30 ` = 38,` 13:31 13:45 ` = 39,` 13:46 14:00 ` = 40,` 14:01 14:15 ` = 41,` 14:16 14:30 ` = 42,` 14:31 14:45 ` = 43,` 14:46 15:00 ` = 44,` 15:01 15:15 ` = 45,` 15:16 15:30 ` = 46,` 15:31 15:45 ` = 47,` 15:46 16:00 ` = 48,` 16:01 16:15 ` = 49,` 16:16 16:30 ` = 50,` 16:31 16:45 ` = 51,` 16:46 17:00 ` = 52,` 17:01 17:15 ` = 53,` 17:16 17:30 ` = 54,` 17:31 17:45 ` = 55,` 17:46 18:00 ` = 56,` 18:01 18:15 ` = 57,` 18:16 18:30 ` = 58,` 18:31 18:45 ` = 59,` 18:46 19:00 ` = 60,` 19:01 19:15 ` = 61,` 19:16 19:30 ` = 62,` 19:31 19:45 ` = 63,` 19:46 20:00 ` = 64,` 20:01 20:15 ` = 65,` 20:16 20:30 ` = 66,` 20:31 20:45 ` = 67,` 20:46 21:00 ` = 68,` 21:01 21:15 ` = 69,` 21:16 21:30 ` = 70,` 21:31 21:45 ` = 71,` 21:46 22:00 ` = 72,` 22:01 22:15 ` = 73,` 22:16 22:30 ` = 74,` 22:31 22:45 ` = 75,` 22:46 23:00 ` = 76,` 23:01 23:15 ` = 77,` 23:16 23:30 ` = 78,` 23:31 23:45 ` = 79,` 23:46 0:00 ` = 80,` 0:01 0:15 ` = 81,` 0:16 0:30 ` = 82,` 0:31 0:45 ` = 83,` 0:46 1:00 ` = 84,` 1:01 1:15 ` = 85,` 1:16 1:30 ` = 86,` 1:31 1:45 ` = 87,` 1:46 2:00 ` = 88,` 2:01 2:15 ` = 89,` 2:16 2:30 ` = 90,` 2:31 2:45 ` = 91,` 2:46 3:00 ` = 92,` 3:01 3:15 ` = 93,` 3:16 3:30 ` = 94,` 3:31 3:45 ` = 95,` 3:46 4:00 ` = 96),class = c("haven_labelled","vctrs_vctr","double"))),row.names = c(NA,-6L),class = c("tbl_df","tbl","data.frame"))

解决方法

如果 read_dta 失败,您可以在“labelled”包的帮助下创建一个查找表。请注意,我在这里使用了示例数据框的删节和稍微调整的版本:

图书馆(已标记) 图书馆(tidyverse)

df <- 
    structure(list(sn1 = structure(c(1,1,1),label = " point number  ",format.stata = "%8.0g"),wjob12s5 = structure(c(NA,NA,2,3),label = " day 5/ twelfth job line starts at  ",format.stata = "%8.0g",labels = c(` 4:01 4:15 ` = 0,` 4:16 4:30 ` = 2,` 4:31 4:45 ` = 3),class = c("haven_labelled","vctrs_vctr","double"))),row.names = c(NA,-6L),class = c("tbl_df","tbl","data.frame"))

lookup_table <- data.frame(
                   code = val_labels(df$wjob12s5),labels =  val_labels(df$wjob12s5) %>% names())

df %>% left_join(lookup_table,by = c("wjob12s5" = "code"))

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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-