creating-micro-level-data-for-SPSS

For univariate analysis of factors:

religion <- c(rep("buddist", 5), rep("hindu", 9), rep("kristen", 21), rep("muslim", 14), rep("jude", 2), rep("ateist", 10), rep("other with weird characters åäö", 5))
foo <- data.frame(religiös=religion)
write.csv(foo, file="religion.csv", row.names=F)
system("recode utf-8..lat1 religion.csv")

The last line recodes form UTF-8 to latin1 since SPSS does not handle UTF-8 well. If you don't use R in UTF-8, then you must omit that line.

For bivariate analysis of factors:

row.categories <- c("arbetare", "tjänstemän", "akademiker")
row.name <- "Klass"
column.categories <- c("ja", "nej")
col.name <- "Attityd"

matrix.of.frequencies <- matrix(c(102, 238, 29, 194, 12, 135), byrow = T, nrow = length(row.categories))
## OR
matrix.of.percentages <- matrix(c(30, 70.2, 13.25, 87, 8.5, 92), byrow = T, nrow = length(row.categories))
row.sums <- c(340, 223, 147)

for(i in 1:length(row.sums)){
  matrix.of.frequencies[i,] <- matrix.of.percentages[i,]/100*row.sums[i]
}

## create micro data
my.data.frame <- data.frame(row.name=NA, col.name=NA)
colnames(my.data.frame) <- c(row.name, col.name)
for(i in 1:length(row.categories)){
  for(j in 1:length(column.categories)){
    temp <- matrix(rep(c(row.categories[i], column.categories[j]), matrix.of.frequencies[i,j]), ncol = 2, byrow = T)
    colnames(temp) <- c(row.name, col.name)
    my.data.frame <- rbind(my.data.frame, temp)
  }
}
my.data.frame <- my.data.frame[-1,]

write.csv(my.data.frame, file = "bivariate.csv", row.names=F)
system("recode utf-8..lat1 bivariate.csv")

comments powered by Disqus


Back to the index

Blog roll

R-bloggers, Debian Weekly
Valid XHTML 1.0 Strict [Valid RSS] Valid CSS! Emacs Muse Last modified: oktober 17, 2019