【问题标题】:Importing TSV file from qualtrics into R raises error将 TSV 文件从 qualtrics 导入 R 会引发错误
【发布时间】:2022-01-29 12:44:19
【问题描述】:

由于错误(该文件从 Qualtrics 导出为 TSV),我无法在 R 中导入 TSV 调查数据文件。这是我的代码:

library(readr)
df <- read_tsv('example_data_from_qualtrics.tsv')

运行代码会导致此错误消息:

Error in vroom_(file, delim = delim %||% col_types$delim, col_names = col_names,  :          
  embedded nul in string: 'S\0t\0a\0r\0t\0D\0a\0t\0e'

可能是编码问题。我还没有找到解决方案。

编辑:这里有一些示例数据:https://ufile.io/9liydp15

【问题讨论】:

  • 如果没有文件样本作为开始,很难为您提供帮助
  • 是的,没错,但我就是不知道如何在堆栈溢出时共享样本数据。我将尝试找出共享示例数据的选项。
  • 您只需将文件的第一行粘贴到帖子的三个反引号内。
  • 数据文件有大量的列,所以甚至无法粘贴标题行。因此我编辑了我的问题并添加了一个下载示例数据的链接,即ufile.io/9liydp15

标签: r csv


【解决方案1】:

感谢您的文件。我认为这是一个有点奇怪的文件,但是有一个编码选项(文件是带有 BOM 的 UTF-16),你可以阅读它:

(见 sublime text,notepad++)。

readr::read_tsv('~/Downloads/example_data_from_qualtrics.tsv', 
                locale = readr::locale(encoding = "UTF-16LE"))

我认为最好在导入文件之前删除文件中的第 2 行和第 3 行。可以在另一个步骤中读取这些行以获得列的标签,例如使用 readr::read_tsv(.., n_max = 2)。

然后readr会更准确地猜测列类型。

要按行获得答案,您可以按用户旋转数据:

u <- readr::read_tsv('~/Downloads/example_data_from_qualtrics.tsv', locale = readr::locale(encoding = "UTF-16LE"))

v <- u %>% tidyr::gather(var, val, - IPAddress, - ResponseId, - UserLanguage, - DistributionChannel, 
                    - StartDate, - EndDate, - RecordedDate, 
                    - RecipientLastName, - RecipientFirstName, - RecipientEmail, - ExternalReference,
                    - Status, - Progress, - `Duration (in seconds)`, - Finished, - LocationLatitude, -LocationLongitude)

要获得标签(和调查问题),您可以:

u <- readr::read_tsv('~/Downloads/example_data_from_qualtrics.tsv', 
                     locale = readr::locale(encoding = "UTF-16LE"), n_max = 3)

v <- u %>% 
  filter(row_number() %in% c(1,3)) %>% 
  tidyr::gather(var, label_question)

【讨论】:

  • 感谢您的全面回答,它运行良好!
  • 很好,感谢您的反馈!
猜你喜欢
  • 1970-01-01
  • 2022-11-04
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-17
相关资源
最近更新 更多