小编mal*_*atr的帖子

合并(rbind)数据帧并使用原始数据帧的名称创建列

我有几个数据框,我想按行组合.在生成的单个数据框中,我想创建一个新变量,用于标识观察来自哪个数据集.

# original data frames
df1 <- data.frame(x = c(1, 3), y = c(2, 4))
df2 <- data.frame(x = c(5, 7), y = c(6, 8))

# desired, combined data frame
df3  <- data.frame(x = c(1, 3, 5, 7), y = c(2, 4, 6, 8),
                   source = c("df1", "df1", "df2", "df2")
# x y source
# 1 2    df1
# 3 4    df1
# 5 6    df2
# 7 8    df2
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?提前致谢!

r

24
推荐指数
4
解决办法
1万
查看次数

dplyr链中的格式列

我有这个数据集:

dat <- 
structure(list(date = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L), .Label = c("3/31/2014", "4/1/2014", "4/2/2014", 
"4/3/2014"), class = "factor"), site = structure(c(1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("a", "b"), class = "factor"), 
clicks = c(73L, 64L, 80L, 58L, 58L, 61L, 70L, 60L, 84L, 65L, 
77L), impressions = c(55817L, 78027L, 77017L, 68797L, 92437L, 
94259L, 88418L, 55420L, 69866L, 86767L, 92088L)), .Names = c("date", 
"site", "clicks", "impressions"), class = "data.frame", …
Run Code Online (Sandbox Code Playgroud)

r date-formatting dplyr

15
推荐指数
2
解决办法
2万
查看次数

dplyr链中的write.csv()

我之前遇到过这个问题,但只是在链外写了导出函数.有没有办法write.csvdplyr链中包含一个语句?

library(dplyr)

data_set %>%
filter(Date == Sys.Date() - 1 | Date == Sys.Date()) %>%
write.csv('data_set_twodays.csv', row.names = F) %>%
filter(Date = Sys.Date()) %>%
write.csv('data_set_today.csv', row.names = F)
NULL
Run Code Online (Sandbox Code Playgroud)

csv file-io r dplyr

11
推荐指数
2
解决办法
9055
查看次数

ggplot/GGally - 平行坐标 - y轴标签

有谁知道是否有办法ggparcoord在GGally中为函数添加变量标签?我尝试了很多方法geom_text,但没有任何结果.

为了更明确,我希望row.names(mtcars)通过geom_text.我可以区分汽车的唯一方法是row.names(mtcars)通过groupColumn论证,但我不喜欢这种看法.

不起作用:

mtcars$carName <- row.names(mtcars) # This becomes column 12
library(GGally)
# Attempt 1
ggparcoord(mtcars, 
           columns = c(12, 1, 6), 
           groupColumn = 1) +
geom_text(aes(label = carName))

# Attempt 2
ggparcoord(mtcars, 
           columns = c(12, 1, 6),
           groupColumn = 1,
           mapping = aes(label = carName))
Run Code Online (Sandbox Code Playgroud)

任何想法,将不胜感激!

r data-visualization ggplot2 parallel-coordinates

9
推荐指数
1
解决办法
840
查看次数

从ggplot2中的geom_smooth中删除线条

我试图从geom_smooth中删除回归线,只保留置信区间.我曾尝试size = 0,size = NULLsize = NA,但没有工作.有没有人知道的简单解决方法?

baseball <- ddply(x, .(id), transform, bat.avg = h/ab)
hank <- subset(baseball, id == 'aaronha01')
ggplot(hank, aes(x = year, y = bat.avg)) +
  geom_line(size = 1.2, color = '#336699') +
  geom_smooth(fill = '#bf3030', size = NA) +
  labs(x = '', y = '')
Run Code Online (Sandbox Code Playgroud)

Hank Aaron的击球率

r ggplot2

6
推荐指数
1
解决办法
2878
查看次数

从闪亮的应用程序创建闪亮的演示文稿?

我找到了这个 - http://shiny.rstudio.com/gallery/download-knitr-reports.html - 可以创建静态PDF,HTML和Word文档的很棒的例子.

我想要做的是上传一个数据集,然后生成一个模板化的闪亮演示文稿.我尝试了多条路线但收效甚微.我得到的最远的是将此代码包含在我的markdown文件中:

---
title: "shinyPresentation"
author: "maloneypatr"
date: "Wednesday, September 03, 2014"
output:
  ioslides_presentation:
    self_contained: false
    lib_dir: libs
runtime: shiny
---
Run Code Online (Sandbox Code Playgroud)

在尝试下载示例.HTML文件时,我收到此错误path for html_dependency not provided.我在追逐目前不存在的功能吗?如果没有,有没有人有一些建议?

提前致谢!

- 易慧评论后的更新 -

>library(rmarkdown);library(shiny);sessionInfo()

R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rmarkdown_0.2.47    knitr_1.6           shiny_0.10.1        ggmap_2.3           googleVis_0.5.5    
[6] stringr_0.6.2 …
Run Code Online (Sandbox Code Playgroud)

r r-markdown shiny

4
推荐指数
1
解决办法
3060
查看次数

从上传的文件中创建动态ggvis图表

我想用Shiny和ggvis来:

1)上传数据集

2)让用户选择2列(x,y)

3)从上传的数据集中返回显示(x,y)的ggvis图

我已经尝试过编辑Shiny Interactivity页面中的示例以及电影资源管理器示例.但是,不显示任何图表.

我认为我的问题是上传数据集,但我不知道从哪里开始...有什么建议吗?

注意 - 我也尝试使用rCharts,但我遇到了类似的问题,没有显示图表.

server.R

library(shiny)
library(dplyr)
library(ggvis)

shinyServer(function(input, output, session) {

fileHeaderNames <- reactive({

  infile <- input$datfile

  if(is.null(infile))
    return(NULL)

  d <- read.csv(infile$datapath, header = T)
  return(names(d))

})

# dynamic variable names
observe({

  updateSelectInput(session, 'x', choices = fileHeaderNames())
  updateSelectInput(session, 'y', choices = fileHeaderNames())

}) # end observe

  # uploading data set
  theData <- reactive({ 

    validate(
       need(input$datfile != "", "Please upload a file")
    )

    infile <- input$datfile
    dat <- read.csv(infile$datapath, 
                    header = …
Run Code Online (Sandbox Code Playgroud)

r shiny rcharts ggvis

4
推荐指数
1
解决办法
1802
查看次数

在R中创建连接矩阵,计算共享成员资格

假设我在R中有一个数据集,表明国际组织中国家的成员资格(原始数据集可以在这里找到:IGO_stateunit_v2.3.zip).

以下是数据基本结构的示例:

cntr <- c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')
UNO <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1)
APEC <- c(0, 0, 0, 0, 1, 1, 1, 0, 0, 0)
ASEAN <- c(0, 0, 0, 0, 1, 1, 0, 0, 0, 0)
data <- data.frame(cntr, UNO, APEC, ASEAN)
Run Code Online (Sandbox Code Playgroud)

所以数据看起来像这样,其中1 =组织中的成员资格:

cntr UNO APEC ASEAN
A   0    0     0
B   1    0     0
C   1    0     0
D   1    0     0 …
Run Code Online (Sandbox Code Playgroud)

r connectivity matrix

3
推荐指数
1
解决办法
124
查看次数