首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将rqda文件转换为sql文件

将rqda文件转换为sql文件
EN

Stack Overflow用户
提问于 2017-05-14 22:24:38
回答 1查看 231关注 0票数 0

我正在使用RQDA,这是一个包在rstudio,以编码文本手动。最后一个rqda文件是一个sql数据库.我用文本编码语句,并使用不同的代码,并将它们归入代码类别(例如:代码类别"actor_party“,以及相关代码”社会主义“、”自由主义“、”保守“等)。我完成了编码,并想用它执行一个社交网络分析。为此,我想要创建一个sql数据库,这样每个代码类别都会得到自己的列,并在每一行中都有特定的代码。每个代码都可以通过以下属性来标识: catid (=代码类别编号)、fid (文件标识号)和selfirst (每个代码的开始)。通过这样做,将为每个编码语句选择特定的catid、fid和selfirst,以便sqlite可以将每个编码标识为唯一的(此外,正如您在下面的R脚本中所看到的,必须为每个有效编码选择status=1 )。

我在0.99.879版本中使用rstudio,在0.2-7版本中使用rqda,在方晶石 1.0.0中使用方晶石1.0.0。

因此,使用以下R代码:

代码语言:javascript
复制
library(RSQLite) # load Package RSQLite
setwd("C:/...")

system("ls *.rqda", show=TRUE)
sqlite <- dbDriver("SQLite")
#specifing the file
qdadb <- dbConnect(sqlite,"My_data.rqda")


dbListTables(qdadb)
dbListFields(qdadb, "coding") # that's where the codings are stored


catid <- dbGetQuery(qdadb, "select distinct(catid) from treecode where status = 1 ORDER BY catid")
i <- 1
table <- dbGetQuery(qdadb, "select fid, selfirst from coding where status = 1 GROUP BY fid, selfirst")
while(i <= max(catid)) {
   ids <- dbGetQuery(qdadb, paste("select cid from treecode where (catid = ",i," and status = 1)", sep=""));
   t <- dbGetQuery(qdadb, paste("select cid, fid, selfirst from coding where (cid in (", paste(as.character(ids$cid), sep="' '", collapse=","), ") and status = 1)", sep=""));
   table <- merge(table, t, by = c("fid","selfirst"), all.x = T);
   i <- i + 1;
   }
# warnings are created because of the same columns which are duplicated by the merging

colnames(table) <- c("fid", "selfirst", dbGetQuery(qdadb, "select name from codecat where status = 1")[,1]) #each code has attributed a unique f(ile)id and selfirst (it's the unique starting point of each coding)

# see below for an example of such a created table

library(car) # Companion to Applied Regression package

# years - catid = 1
table$A00_time_frame <- recode(table$A00_time_frame, '1 = 2010; 2 = 2011; 3 = 2012; 4 = 2013; 5 = 2014; 6 = 2015')

# Sources - catid = 2
ids <- dbGetQuery(qdadb, "select cid from treecode where (catid = 2 and status = 1)")[,1]
values <- dbGetQuery(qdadb, paste("select name from freecode where (id in(", paste(ids, collapse = ","), ") and status = 1)"))[,1]
table$B00_source <- recode(table$B00_source, paste0("'", paste(ids,"'='", values, collapse = "';'", sep=""),"'", sep=""))

# Claimant type - catid = 3
ids <- dbGetQuery(qdadb, "select cid from treecode where (catid = 3 and status = 1)")[,1]
values <- dbGetQuery(qdadb, paste("select name from freecode where (id in(", paste(ids, collapse = ","), ") and status = 1)"))[,1]
table$C00_claimant_type <- recode(table$C00_claimant_type, paste0("'", 
paste(ids,"'='", values, collapse = "';'", sep=""),"'", sep=""))

and so until "catid = 20"

这是可行的,如下所示:表格和这个表一直持续到第844行--只有fid正在上升。

尽管这是可行的,并且创建的表与编码的总数相匹配,但仍会发生一些错误。有些代码没有链接到正确的语句(尽管它们链接到正确的代码类别,但没有链接到正确和编码的语句)

我仍然是R(工作室)的初学者,无法解释哪里出了问题。

有没有人知道这里的问题或错误是什么,以及如何解决?

应要求,我很高兴分享我的档案:)

欢迎任何建议或帮助!

编辑:是我的数据子集的一个链接,您可以复制它(文件是rqda格式的,因为我认为它的转换本身就是问题所在)。

此外,给你两个例子,看看哪里。

通过在R中创建“表”,可以识别下一行

    • fid 95,selfirst 4553,然后编码'Welt‘,然后是'E02_European_Commission’+ 'G10_Cameroon‘ 但是,如果您检查原始rqda-文件中的编码,代码“喀麦隆”不在此文件中,而是在fid 70、selfirst 5082和2010年的Welt中。

    • fid 90、selfirst 959和年份“2011”显示代码“CDU”,最后一行“特别索赔人”显示名称“Martin Schulz”。 但是,如果您检查原始rqda文件中的编码,则子集中的代码'Martin Schulz‘没有附加编码。

我希望,这两个例子说明了这个问题,并给出了一个分别看问题所在的地方。

对不起,我一开始没有提供!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-15 20:17:36

或许可以先简化代码,以便更好地了解可能发生的错误?就我个人而言,我更多地依赖SQL而不是R来整理所有信息:

代码语言:javascript
复制
t <- dbGetQuery(qdadb, "SELECT codecat.name, coding.cid, coding.fid, coding.selfirst 
       FROM treecode, coding, codecat 
       WHERE treecode.cid = coding.cid 
       AND treecode.catid = codecat.catid
       AND treecode.status = 1
       AND coding.status = 1")
head(reshape(t, idvar = c("fid", "selfirst"), timevar = "name", direction = "wide"))

不确定这是你正在寻找的,或它是否更好的工作。但它似乎更简单的代码来评估。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43969388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档