Using R to parse out Surveymonkey csv files

后端 未结 7 1046
说谎
说谎 2021-02-05 12:01

I\'m trying to analyse a large survey created with surveymonkey which has hundreds of columns in the CSV file and the output format is difficult to use as the headers run over t

7条回答
  •  太阳男子
    2021-02-05 12:36

    The issue with the headers is that columns with "select all that apply" will have a blank top row, and the column heading will be the row below. This is only an issue for those types of questions.

    With this in mind, I wrote a loop to go through all columns and replace the column names with the value from the second row if the column name was blank- which has a character length of 1.

    Then, you can kill the second row of the data and have a tidy data frame.

    for(i in 1:ncol(df)){
    newname <- colnames(df)[i]
    if(nchar(newname) < 2){
    colnames(df)[i] <- df[1,i]
    } 
    
    df <- df[-1,]
    

提交回复
热议问题