我正在尝试使用该xlsx包将带有日期列的数据框导出到 Excel 。的帮助文件write.xlsx()说明了格式化日期列的方法。我尝试按照下面的方法进行操作,但生成的 Excel 文件没有显示正确的格式。我可以手动将 Excel 中的单元格格式更改为自定义日期,但这是一个额外的步骤,使其无法重现。
library(lubridate)
library(xlsx)
dat <- data.frame(dates=c("2014-07-16 15:03:16", "2014-07-16 14:52:03", "2014-07-16 16:50:38", "2014-07-12 00:00:00", "2014-07-12 00:00:00"))
dat$dates <- ymd_hms(dat$dates)
wb <- createWorkbook()
saveWorkbook(wb, "output.xlsx")
oldOpt <- options()
options(xlsx.date.format="yyyy-mm-dd HH:mm:ss") # change date format
write.xlsx(dat,
"output.xlsx",
sheetName="output")
options(oldOpt) # revert back to defaults
Run Code Online (Sandbox Code Playgroud)

为什么我的最后一步是将数据帧转换为向量?我想在数据框中保留前6000个观测值key.
set.seed(1)
key <- data.frame(matrix(NA, nrow = 10000, ncol = 1))
names(key) <- "ID"
key$ID <- replicate(10000,
rawToChar(as.raw(sample(c(48:57,65:90,97:122), 8, replace=T))))
key <- unique(key) # still a data frame
key <- key[1:6000,] # no longer a data frame
Run Code Online (Sandbox Code Playgroud) 我试图将每个数据框子集,以排除第一列为NA或""的行.我尝试将数据帧放入列表中df,然后lapply在每个数据帧上使用.代码工作,只是我不知道如何用子集覆盖每个数据帧.
df1 <- data.frame(v1=c(1, 2, 3, NA, NA, NA), v2=rep(1, 6))
df2 <- data.frame(v11=c(2, 3, 4, 5, NA, ""), v22=rep(1, 6))
df3 <- data.frame(v111=c(3, 4, 5, 6, 7, NA), v222=rep(1, 6))
df <- list(df1=df1, df2=df2, df3=df3)
df
$df1
# v1 v2
# 1 1 1
# 2 2 1
# 3 3 1
# 4 NA 1
# 5 NA 1
# 6 NA 1
#
# $df2
# v11 v22
# 1 2 1
# …Run Code Online (Sandbox Code Playgroud) 有没有一种快速的方法来扫描R脚本并确定实际使用哪些包?我的意思是查看脚本中调用的所有函数并返回包含这些函数名的包列表?(我知道函数名不是任何一个包独有的)
为什么不能仅看通过所谓的包library()或require()?对.好吧,我习惯于加载我经常使用的包,无论我是否在脚本中实际使用它们.
我想通过删除未使用的软件包清理一些我打算与他人共享的脚本.
我决心在2016年改变我的方式.请帮助我开始.
更新
评论中的一些好主意......
# create an R file that uses a few functions
fileConn<-file("test.R")
writeLines(c("df <- data.frame(v1=c(1, 1, 1), v2=c(1, 2, 3))",
"\n",
"m <- mean(df$v2)",
"\n",
"describe(df) #psych package"),
fileConn)
close(fileConn)
# getParseData approach
pkg <- getParseData(parse("test.R"))
pkg <- pkg[pkg$token=="SYMBOL_FUNCTION_CALL",]
pkg <- pkg[!duplicated(pkg$text),]
pkgname <- pkg$text
pkgname
# [1] "data.frame" "c" "mean" "describe"
Run Code Online (Sandbox Code Playgroud)
更新2
实施@ nicola的想法是一个丑陋的尝试:
# load all probable packages first
pkgList <- list(pkgname)
for (i in 1:length(pkgname)) {
try(print(packageName(environment(get(pkgList[[1]][i])))))
} …Run Code Online (Sandbox Code Playgroud) 是否可以在knitrRnw块图标题中引用先前定义的对象?
<<chunk1>>=
myObj <- "caption"
@
<<chunk2, fig.cap="This is my \\Sexpr{myObj}">>=
plot(1,2)
@
Run Code Online (Sandbox Code Playgroud)
这个答案显示了如何在图标题中使用LaTeX,但我无法使用相同的想法\Sexpr{}和R对象.
在尝试找到有关保存由 生成的热图的SO 问题的解决方案的过程中d3heatmap,我webshot在(HT: hrbrmstr )的开发版本中knitr遇到了一个无关的问题。
library(devtools)
devtools::install_github('yihui/knitr', build_vignettes = TRUE)
library(knitr)
Run Code Online (Sandbox Code Playgroud)
安装并加载开发版后knitr,我尝试运行以下.Rmd文件:
---
title: "Untitled"
output: pdf_document
---
```{r}
library(d3heatmap)
d3heatmap(mtcars, scale = "column", colors = "Spectral")
```
Run Code Online (Sandbox Code Playgroud)
提示我安装phantomjs:
webshot::install_phantomjs()
phantomjs.exe has been installed to /Users/USERNAME/Library/Application Support/PhantomJS
Run Code Online (Sandbox Code Playgroud)
我.Rmd再次尝试运行该文件,但出现此错误:
sh: ~/Library/Application Support/PhantomJS/phantomjs: No such file or directory
Quitting from lines 7-9 (test.Rmd)
Error in (function (url = NULL, file = "webshot.png", vwidth = 992, vheight = …Run Code Online (Sandbox Code Playgroud) 我试图检测一个打开的文本字段(读:凌乱!)与名称向量之间的匹配.我创造了一个愚蠢的水果例子,突出了我的主要挑战.
df1 <- data.frame(id = c(1, 2, 3, 4, 5, 6),
entry = c("Apple",
"I love apples",
"appls",
"Bannanas",
"banana",
"An apple a day keeps..."))
df1$entry <- as.character(df1$entry)
df2 <- data.frame(fruit=c("apple",
"banana",
"pineapple"),
code=c(11, 12, 13))
df2$fruit <- as.character(df2$fruit)
df1 %>%
mutate(match = str_detect(str_to_lower(entry),
str_to_lower(df2$fruit)))
Run Code Online (Sandbox Code Playgroud)
如果你愿意的话,我的方法会抓住低悬的水果(完全匹配"Apple"和"banana").
# id entry match
#1 1 Apple TRUE
#2 2 I love apples FALSE
#3 3 appls FALSE
#4 4 Bannanas FALSE
#5 5 banana TRUE
#6 6 An apple a day keeps... FALSE …Run Code Online (Sandbox Code Playgroud) 我正在使用 Rmarkdown 并编写 PDF。我想在(a) 不创建列表的情况下开始一个段落。添加\(a)有效,但它突出显示了我的 Rmd 文件文本的其余部分,使其难以阅读。有没有其他方法表明我不想在 Rmarkdown 中创建列表?
---
title: "Untitled"
output:
pdf_document: default
html_document: default
---
This is the first paragraph.
```{r, include=FALSE}
1+1 # nice highlighting before problem
```
(a) This should be the second paragraph. (b) But it is interpreted as a list.
\(a) This slash works to keep the text as a paragraph, but it highlights all remaining text in my Rmd file, which makes it hard to read. (b) Is …Run Code Online (Sandbox Code Playgroud) 我想缩小打印区域,以便为ggrepel当前被切断的标签留出更多空间。我似乎再也无法通过偏移标签nudge_x(),并且我不想缩小文本大小。
我正在尝试寻找一种压缩图表的方法,以使所有组都更靠近中心,从而在x轴的极限处留出更多的标签空间。
具体来说,我正在尝试将此图编织为纵向PDF。我尝试fig.width在块选项中进行控制,但这只是使整个图表变小了。
我想最大化纵向页面上的宽度,但相对于标签区域缩小打印区域。
---
title : "The title"
shorttitle : "Title"
author:
- name : "Me"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Address"
email : "email"
affiliation:
- id : "1"
institution : "Company"
authornote: |
Note here
abstract: |
Abstract here.
floatsintext : yes
figurelist : no
tablelist : no
footnotelist : no
linenumbers : no
mask : no
draft : no
note : …Run Code Online (Sandbox Code Playgroud) 我从左连接中得到了意外的 NA 模式。该数据来自本周的“整洁星期二”。
\nlibrary(tidyverse)\n\nbreed_traits <- readr::read_csv(\'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/breed_traits.csv\') %>%\n select(Breed, `Affectionate With Family`)\n\n# A tibble: 195 \xc3\x97 2\n Breed `Affectionate With Family`\n <chr> <dbl>\n 1 Retrievers (Labrador) 5\n 2 French Bulldogs 5\n 3 German Shepherd Dogs 5\n 4 Retrievers (Golden) 5\n 5 Bulldogs 4\n 6 Poodles 5\n 7 Beagles 3\n 8 Rottweilers 5\n 9 Pointers (German Shorthaired) 5\n10 Dachshunds 5 \n\nbreed_rank_all <- readr::read_csv(\'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/breed_rank.csv\') %>%\n select(Breed, `Rank 2013`)\n\n# A tibble: 195 \xc3\x97 2\n Breed `2013 Rank`\n <chr> <dbl>\n 1 Retrievers …Run Code Online (Sandbox Code Playgroud) r ×10
knitr ×3
dplyr ×1
fuzzywuzzy ×1
ggplot2 ×1
ggrepel ×1
phantomjs ×1
r-markdown ×1
xlsx ×1