2017-02-28 74 views
0

我使用API​​调用LimeSurvey将数据导入我正在处理的Shiny R应用程序。然后,我操纵数据框,以便只有某个人随着时间的推移才能做出回应。数据帧可以是这样的:动态R数据框 - 更改是/否对1/0的响应

Appetite <- c("No","Yes","No","No","No","No","No","No","No") 
Dental.Health <- c("No","Yes","No","No","No","No","Yes","Yes","No") 
Dry.mouth <- c("No","Yes","Yes","Yes","Yes","No","Yes","Yes","No") 
Mouth.opening <- c("No","No","Yes","Yes","Yes","No","Yes","Yes","No") 
Pain.elsewhere <- c("No","Yes","No","No","No","No","No","No","No") 
Sleeping <- c("No","No","No","No","No","Yes","No","No","No") 
Sore.mouth <- c("No","No","Yes","Yes","No","No","No","No","No") 
Swallowing <- c("No","No","No","No","Yes","No","No","No","No") 
Cancer.treatment <- c("No","No","Yes","Yes","No","Yes","No","No","No") 
Support.for.my.family <- c("No","No","Yes","Yes","No","No","No","No","No") 
Fear.of.cancer.coming.back <- c("No","No","Yes","Yes","No","No","Yes","No","No") 
Intimacy <- c("Yes","No","No","No","No","No","No","No","No") 
Dentist <- c("No","Yes","No","No","No","No","No","No","No") 
Dietician <- c("No","No","Yes","Yes","No","No","No","No","No") 
Date.submitted <- c("2002-07-25 00:00:00", 
       "2002-09-05 00:00:00", 
       "2003-01-09 00:00:00", 
       "2003-01-09 00:00:00", 
       "2003-07-17 00:00:00", 
       "2003-11-06 00:00:00", 
       "2004-12-17 00:00:00", 
       "2005-06-03 00:00:00", 
       "2005-12-17 00:00:00") 

theDataFrame <- data.frame(Date.submitted, 
          Appetite, 
          Dental.Health, 
          Dry.mouth, 
          Mouth.opening, 
          Pain.elsewhere, 
          Sleeping, 
          Sore.mouth, 
          Swallowing, 
          Cancer.treatment, 
          Support.for.my.family, 
          Fear.of.cancer.coming.back, 
          Intimacy, 
          Dentist, 
          Dietician) 

需要明确的是,这数据帧可以包含更多(或更少)比上面的例子更多(或更少)的变量的观察。

我的目标是做一个动态直方图,看起来像下面这样:

library(dplyr) 
library(ggplot2) 
library(tidyr) 

df <- data.frame(timeline = Sys.Date() - 1:10, 
       q3 = sample(c("Yes", "No"), size = 10, replace = T), 
       q4 = sample(c("Yes", "No"), size = 10, replace = T), 
       q5 = sample(c("Yes", "No"), size = 10, replace = T), 
       q6 = sample(c("Yes", "No"), size = 10, replace = T), 
       q7 = sample(c("Yes", "No"), size = 10, replace = T), 
       q8 = sample(c("Yes", "No"), size = 10, replace = T), 

       stringsAsFactors = F) %>% 
    mutate(q3 = ifelse(q3 == "Yes", 1, 0), 
      q4 = ifelse(q4 == "Yes", 1, 0), 
      q5 = ifelse(q5 == "Yes", 1, 0), 
      q6 = ifelse(q6 == "Yes", 1, 0), 
      q7 = ifelse(q7 == "Yes", 1, 0), 
      q8 = ifelse(q8 == "Yes", 1, 0) 

    ) %>% 
    gather(key = question, value = value, q3, q4, q5, q6, q7, q8) 

g <- ggplot(df, aes(x = timeline, y = value, fill = question)) + 
    geom_bar(stat = "identity") 

g 

我想我会需要使用库(lubridate)的时间表,因为整个数据帧是纯文本。我处理''。列名如下:

myColNames <- colnames(theDataFrame) 

myNames <- myColNames 

myNames <- gsub("^X\\.\\.", "", myNames) 
myNames <- gsub("\\.", " ", myNames) 
names(theDataFrame) <- myNames # items in myChoices get "labels" from myNames 

但是最具挑战性的方面是让它动态工作。该数据集将只包含Date.submitted和(x)额外列的数目将只是“是”或“否”

我希望我已经提供了足够的信息(这是我在Stack Exchange上的第一个问题!)

回答

1

我们可以通过base R

theDataFrame[-1] <- +(theDataFrame[-1]=="Yes") 

或用lapply更新时,该数据集是大

theDataFrame[-1] <- lapply(theDataFrame[-1], function(x) as.integer(x=="Yes")) 
+0

就这一点,我已经得到了,但我似乎无法让我周围的最后一点头......我会发布一个新的问题 –

+0

,然后这个不起作用... | ... stringsAsFactors = F)%>% gather(%data_frame [-1],(x)as.integer(x ==“是”))%>% gather key = question,value = value,as.list(theDataFrame)) is.character(x)不是TRUE我很困惑 –

+0

@JasonJorgenson不确定我收到你的第二条评论 – akrun

0

你可以人所以使用dplyr::mutate_allpurrr::map

注:我以前stringsAsFactors = FtheDataFrame

theDataFrame <- data.frame(Date.submitted, 
          Appetite, 
          Dental.Health, 
          Dry.mouth, 
          Mouth.opening, 
          Pain.elsewhere, 
          Sleeping, 
          Sore.mouth, 
          Swallowing, 
          Cancer.treatment, 
          Support.for.my.family, 
          Fear.of.cancer.coming.back, 
          Intimacy, 
          Dentist, 
          Dietician, stringsAsFactors = F) 

- 创建一个函数来完成你想要的转换,例如:

ConvertYesNo<- function(x){ 
    if(x=="Yes") y <- as.integer(1) 
    else if (x=="No") y <- as.integer(0) 
    else y <- x 

    return(y) 
} 

- 使用它mutate_all,其中考虑所有列或选择使用mutate_at的列。而map的功能如下:

theDataFramex <- theDataFrame %>% 
    mutate_all(funs(map_chr(.,ConvertYesNo))) 

> head(theDataFramex,3) 
     Date.submitted Appetite Dental.Health Dry.mouth Mouth.opening Pain.elsewhere Sleeping 
1 2002-07-25 00:00:00  0    0   0    0    0  0 
2 2002-09-05 00:00:00  1    1   1    0    1  0 
3 2003-01-09 00:00:00  0    0   1    1    0  0 
    Sore.mouth Swallowing Cancer.treatment Support.for.my.family Fear.of.cancer.coming.back 
1   0   0    0      0       0 
2   0   0    0      0       0 
3   1   0    1      1       1 
    Intimacy Dentist Dietician 
1  1  0   0 
2  0  1   0 
3  0  0   1 
+0

谢谢OmaymaS。这很有帮助! –

相关问题