Translations of this page:
 

Import Data to R

These instructions describe how to import the collected data into R Project for Statistical Computing. You essentially have two options here.

1. you can retrieve the data directly from R.

2. you can download a script and a CSV file in SoSci Survey and import them.

The same thing happens in the background: an R script imports a CSV file. However, the first option is much more convenient.

In both cases, the variables are provided with a description using comment(), and the values of selection questions are stored as factor unless otherwise set. For scale questions, the answer options are labeled using attr().

> comment(ds$SERIAL)
[1] "Personal identifier or participation code (if used)"
 
 
> levels(ds$AB01)
[1] "Yes, regularly"                    "From time to time"
[3] "No or only in exceptional cases"   "[NA] not answered" 
 
> attr(ds$SC01_01, "1")
[1] "does not apply at all"

Import via API

You can use either a temporary or a permanent URL for the API download.

  • Temporary URL:
    Collected dataDownload data → tab GNU R → button Enable API import.
  • Permanent URL:
    Collected dataData retrieval via API → plus symbol top right → function Retrieve data as CSV for R → Save
    (you will then find the command in the line Command in the overview)

In both cases, SoSci Survey provides a short code that looks like this

eval(parse("https://www.soscisurvey.de/rtr/?act=f20H7pQDcp1OCdseFRmCUSmj&vQuality&rScript", encoding="UTF-8"))

This code retrieves a script directly from the URL, interprets it as an R script and executes it. Copy this command into your R console or into an R script and execute it.

Tip: If you encounter problems, you can also call up the script directly in the browser via the specified URL, copy it to R and execute it there.

https://www.soscisurvey.de/rtr/?act=f20H7pQDcp1OCdseFRmCUSmj&vQuality&rScript

Import via Download

Select the “GNU R” tab under Collected dataDownload data and download both the R script (R file) and the CSV file (Download data: GNU R).

Note: If the data and the script were downloaded at different times, it may be that the files are incompatible (e.g. because the list of variables used may have changed).

Note: When downloading the survey data, choose the GNU R file format instead of CSV. This ensures that the formatting in the file will be compatible with the import script.

The following steps describe the process when using the RGui user interface for R. In principle, the process is virtually the same if you are using R from the console, or another GUI such as RStudio.

Open and start the script in R:

  • FileOpen script
  • EditRun all
  • A dialog should now open that allows you to select the file (this may be behind the window currently open). Select the CSV file that you downloaded and confirm with Open.

The survey data should be be located in a data frame that you specified when downloading the script (default is “data”). You can check the variables using the R command str(data).

Notes

It has already been pointed out above that the R import labels the variables using comment(). You can use the Varlist() function from the R-Helpers to display a variable overview for your data.frame, as is available in SPSS, for example.

To ensure that this information is not lost during subselects, the import script adds an avector class by default, which copies the attributes during the subselect. If the avector becomes a problem with a bind_rows(), for example, you can remove this class with the following function.

remove_avector = function(x) {
    x = data.frame(
      lapply(x, function(xx) {
        class(xx) = setdiff(class(xx), c("avector"))
        return(xx)
      })
    )
    return(x)
}
 
dc <- remove_avector(ds)

When downloading via CSV file and with the temporary API link, you can switch the additional function on and off using the option “Add function to retain variable descriptions in subsets”.

en/results/import-gnu-r.txt · Last modified: 28.05.2024 15:56 by kraigerlukas
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Driven by DokuWiki