Sea*_*ean 15 csv parsing r surveymonkey
我正在尝试分析使用surveymonkey创建的大型调查,该调查在CSV文件中有数百列,并且输出格式很难使用,因为标题会在两行上运行.
谢谢!
我最后做的是使用标有V1,V2等的libreoffice打印出标题然后我只是在文件中读取为
m1 <- read.csv('Sheet1.csv', header=FALSE, skip=1)
Run Code Online (Sandbox Code Playgroud)
然后对m1 $ V10,m1 $ V23等进行了分析......
为了解决多列的混乱,我使用了以下小功能
# function to merge columns into one with a space separator and then
# remove multiple spaces
mcols <- function(df, cols) {
# e.g. mcols(df, c(14:18))
exp <- paste('df[,', cols, ']', sep='', collapse=',' )
# this creates something like...
# "df[,14],df[,15],df[,16],df[,17],df[,18]"
# now we just want to do a paste of this expression...
nexp <- paste(" paste(", exp, ", sep=' ')")
# so now nexp looks something like...
# " paste( df[,14],df[,15],df[,16],df[,17],df[,18] , sep='')"
# now we just need to parse this text... and eval() it...
newcol <- eval(parse(text=nexp))
newcol <- gsub(' *', ' ', newcol) # replace duplicate spaces by a single one
newcol <- gsub('^ *', '', newcol) # remove leading spaces
gsub(' *$', '', newcol) # remove trailing spaces
}
# mcols(df, c(14:18))
Run Code Online (Sandbox Code Playgroud)
毫无疑问,有人能够清理它!
为了整理我使用的Likert式秤:
# function to tidy c('Strongly Agree', 'Agree', 'Disagree', 'Strongly Disagree')
tidylik4 <- function(x) {
xlevels <- c('Strongly Disagree', 'Disagree', 'Agree', 'Strongly Agree')
y <- ifelse(x == '', NA, x)
ordered(y, levels=xlevels)
}
for (i in 44:52) {
m2[,i] <- tidylik4(m2[,i])
}
Run Code Online (Sandbox Code Playgroud)
随意评论,毫无疑问,这将再次出现!