如何将调查回复的数据帧转换为频率表?

时间:2019-02-10 04:46:57

标签: r dataframe dplyr

我有一个调查结果的R数据框。每列都是对调查问题的答复。它可以取值1到10和NA。我想把它变成频率表。

这是我拥有的数据的示例。我假装值从1到3,而不是1到10。

data.frame(
  "Person" = c(1,2,3),
  "Question1" = c(NA, "1", "1"),
  "Question2" = c("1", "2", "3")
)

我想要什么:

data.frame(
  "Question" = c("Question1", "Question2"),
  "Frequency of 1" = c(2, 1),
  "Frequency of 2" = c(0 , 1),
  "Frequency of 3" = c(0, 1)
)

我已经尝试过使用likert包中的likert(),但得到的分数结果不正确。有解决这个问题的简单方法吗?

4 个答案:

答案 0 :(得分:3)

这是使用dplyr和purrr软件包的解决方案

library(dplyr)
library(purrr)

data.frame(
  "Person" = c(1,2,3),
  "Question1" = c(NA, "1", "1"),
  "Question2" = c("1", "2", "3")
)

df %>% 
  select(-Person) %>% 
  mutate_all(~ factor(.x, levels =  as.character(1:10) ) %>% addNA() ) %>% 
  map(table) %>% 
  transpose() %>% 
  map(as.integer) %>% 
  set_names( ~ paste0("Frequency of ",ifelse(is.na(.), "NA", .))) %>% 
  as_tibble() %>% 
  mutate(Question = setdiff(names(df),"Person")) %>% 
  select(Question,everything(), "Frequency of NA" = `Frequency of ` ) 

答案 1 :(得分:2)

一种data.table解决方案:

require(data.table)
setDT(df)    

# Melt data:
df <- melt(df, id.vars = "Person", value.name = "Question")

# Cast data to required structure:
df <- data.frame(dcast(df, variable ~ Question))

# Rename variables and remove NA count (as per Ops question):
names(df)[1] <- "Question"
names(df)[-1] <- gsub("X", "Frequency of ", names(df)[-1])
df$NA. <- NULL

df
#   Question Frequency of 1 Frequency of 2 Frequency of 3
#1 Question1              2              0              0
#2 Question2              1              1              1

或单行答案:

dcast(melt(setDT(df), id.vars="Person", value.name="Question")[!Question %in% NA][, Question := paste0("Frequency of ", Question)], variable ~ Question)

答案 2 :(得分:1)

另一种tidyverse可能性是:

df %>%
 gather(Question, val, -Person, na.rm = TRUE) %>%
 group_by(Question, val) %>%
 summarise(res = length(val)) %>%
 ungroup() %>%
 mutate(val = paste0("Frequency.of.", val)) %>%
 spread(val, res, fill = NA)

  Question  Frequency.of.1 Frequency.of.2 Frequency.of.3
  <chr>              <int>          <int>          <int>
1 Question1              2             NA             NA
2 Question2              1              1              1

首先,将数据从宽格式转换为长格式。其次,它根据问题计算频率。最后,它创建“ Frequency.of”。变量并将数据返回其所需的形状。

或者,如果您还想计算每个问题的NA值:

df %>%
 gather(Question, val, -Person) %>%
 group_by(Question, val) %>%
 summarise(res = length(val)) %>%
 ungroup() %>%
 mutate(val = paste0("Frequency.of.", val)) %>%
 spread(val, res, fill = NA)

  Question  Frequency.of.1 Frequency.of.2 Frequency.of.3 Frequency.of.NA
  <chr>              <int>          <int>          <int>           <int>
1 Question1              2             NA             NA               1
2 Question2              1              1              1              NA

答案 3 :(得分:0)

这不是最优雅的方法,但可能会有所帮助:df2是您的数据集。 数据:

   df2<-data.frame(
  "Person" = c(1,2,3),
  "Question1" = c(NA, "1", "1"),
  "Question2" = c("1", "2", "3"),stringsAsFactors = F
)

目标: 编辑::您可以按照以下步骤“自动化”

df2[is.na(df2)]<-0 #To allow numeric manipulation
values<-c("1","2","3")
    Final_df<-sapply(values,function(val) apply(df2[,-1],2,function(x) sum(x==val)))
    Final_df<-as.data.frame(Final_df)
    names(Final_df)<-paste0("Frequency of_",1:ncol(Final_df))

这将产生:

             Frequency of_1          Frequency of_2          Frequency of_3
Question1              2                0                    0
Question2              1                1                    1