[R-mini] Convert a long string to a named list

Suppose I would like to convert this string, "N=100, NRep=200, K=10, P=5", to a named list, I can use eval() and parse(). This is useful when the arguments for a function is passed as one single string as the command line argument.
str <- "N=100,NRep=200,K=10,P=5"
x <- eval(parse(text=paste('list(', str, ')')))

This is the content of x:

$N
[1] 100
$NRep
[1] 200
$K
[1] 10
$P
[1] 5

I believe I learned about this somewhere, but I forgot the source.

If a value in the list is a string, just use single quotes, for example:

str <- "N=100,NRep=200,name='Test',P=5"

[R-mini] Read an SPSS data file

I still occasionally need to read an SPSS data file in R because some of my collaborators use SPSS. I usually use read.spss() from the foreign package:

library(foreign)
fromSPSS.df <- read.spss(file.choose(), reencode=65001, use.value.labels=TRUE, to.data.frame=TRUE)

The output is the data frame fromSPSS.df.

file.choose() can be replaced by the file name in quotes if the SPSS data file in the working directory.

reencode=65001 is not necessary if all the content in the SPSS data file is in English. However, I found that I need this option to import some SPSS data files with Chinese characters correctly.

By default, use.value.labels=TRUE. I put it here to remind me of this argument, such that I can turn it off if necessary. For example, when some variables are Likert scale scores with value labels but should be treated as numbers.

I usually work with data frame and so I use to.data.frame=TRUE. This also converts all missing values to NA.

read.spss() is very old and cannot read some SPSS files I have. But it is good enough for most purposes.

More on read.spss() at inside-R.org.

[R-mini] Scatter plots for Likert scales data in psychological studies: Binned scatter plot?

As mentioned in a previous post,
"Scatter plot is an important tool for data exploration. However, Likert scales, with only a few discrete possible values, are common in psychology. The usual scatter plot is not good for examining the association between two Likert scale variables. Cross-tabulation table can show the frequencies, but I like using graphs."
Bubble plot is introduced in that post. This post introduces binned scatter plot, using the shading to reflect the frequency of a cell. This is the sample code:



This is the result:



Latest version can be found at RNotes at GitHub.