如何将Qualtrics数据(以csv格式)导入R中 [英] How to import Qualtrics data (in csv format) into R

查看:366
本文介绍了如何将Qualtrics数据(以csv格式)导入R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将从Qualtrics下载的数据导入R.它是一个csv文件。

I am trying to import a data downloaded from Qualtrics into R. It is a csv file.

但是,我遇到了2个问题。

However, I encounter 2 problems.


  1. R无法自行确定每列的格式,可能是因为第2行和第3行(上面突出显示)都是无用的文本。 R认为所有列都是字符。但是,显然有些是 date ,有些是 factor ,有些是整数。 R如何正确地计算出每列的数据类?

  1. R could not figure out the format of each column by itself, probably because row 2 and row 3 (highlighted above) are all useless text. R thinks that all columns are character. However, obviously some are date, some are factor, and some are integer. How can R figure out the data class of each column correctly by itself?




library(tidyverse)
filename <- "mydata.csv"
df = read_csv(filename, col_names = TRUE)

Parsed with column specification:
cols(
  .default = col_character()
)
See spec(...) for full column specifications.





  1. I还尝试分别加载变量名称( header )和数据矩阵。不幸的是,使用 skip = 3 参数不起作用。它说我的数据只有1个观察...为什么?

  1. I also tried to load the variable name (header) and data matrix separately. Unfortunately, using the skip = 3 argument does not work. It says that my data only has 1 observation... Why?




 filename <- "mydata.csv"
 headers = read_csv(filename, col_names = FALSE, n_max = 1)
 df = read_csv(filename, skip = 3, col_names = FALSE)
 colnames(df)= headers




Error in names(x) <- value : 
'names' attribute [273] must be the same length as the vector [1]

将我的csv文件导入R的好方法是什么?

What is a good way to import my csv file into R?

推荐答案

我使用以下代码将数据从Qualtrics导入R:

I use the following code to import data from Qualtrics into R:

library(tidyverse)
filename <- "mydata.csv"
headers = read_csv(filename, col_names = FALSE, n_max = 1)
df = read_csv(filename, skip = 3, col_names = FALSE)
colnames(df)= headers

但是,有一点需要注意。 此方法仅在您下载数据时删除所有换行符时才有效。 (请参阅下图,了解如何操作。)我的 skip = 3 参数有效,因为我从Qualtrics下载数据时删除了所有换行符。您在Qualtrics中询问的问题很可能包含多行。 R以这种方式理解您的文件是一个问题。我建议您从网站下载数据时删除所有换行符。

However, there is one caveat. This method only works when you removed all line breaks when you downloaded your data. (Please see the graph below as to how to do so.) My skip = 3 argument works because I removed all line breaks when I downloaded the data from Qualtrics. It is very probable that the questions you asked in Qualtrics contains multiple lines. It constitutes a problem for R to understand your file in this way. I recommend you to remove all line breaks when you download the data from the website.

使用上面的方法,R通常可以正确识别大多数列的数据结构,为您节省了大量精力进行自我重新编码。

Using the method above, R can normally correctly recognise the data structure of most columns, saving yourself a ton of effort to recode yourself.

这篇关于如何将Qualtrics数据(以csv格式)导入R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆