例子:
---
output: pdf_document
mainfont: Times New Roman
fontsize: 12pt
spacing: 1.1
geometry: "left=1in,right=1in,top=0.5in,bottom=1in"
vspace: 4pt
---
This line is normal text.
This line uses Garamond font and font size 14.
This line uses arial font and font size 16.
This line is normal text.
Run Code Online (Sandbox Code Playgroud)
我尝试了几种选择,但没有一个有效。请告诉我是否有办法做到这一点。
例如,
var <- structure(character(3), names=character(3))
v2 <- "p2"; names(v2) <- "name p2"
Run Code Online (Sandbox Code Playgroud)
如果我执行以下操作:
var[2] <- v2
Run Code Online (Sandbox Code Playgroud)
仅将值“p2”传递到向量中,而不将名称“name p2”传递到向量中。
我想要的是一种单行语法来执行以下操作:
var[2] <- v2; names(var)[2] <- names(v2)
Run Code Online (Sandbox Code Playgroud) 我有以下数据:
library(tidyverse)
set.seed(1)
test <- data.frame(id = c(rep(1, 3), rep(2, 4), rep(3, 5)),
Year = 2000 + c(1,3,5,2,3,5,6,1,2,3,4,5),
var1 = sample(0:2, replace = TRUE, size = 12, prob = c(0.6, 0.3, 0.1)),
var2 = sample(0:2, replace = TRUE, size = 12, prob = c(0.6, 0.3, 0.1)))
Run Code Online (Sandbox Code Playgroud)
我需要在每个 id 组中每个变量 ( var1and var2) 不为零的第一年。
我知道如何找到第一个非零行的行号:
temp <- function(a) ifelse(length(head(which(a>0),1))==0,0,head(which(a>0),1))
test2 <- test %>% group_by(id) %>%
mutate_at(vars(var1:var2),funs(temp)) %>%
filter(row_number()==1) %>% select (-year)
id var1 var2
1 1 0 1
2 …Run Code Online (Sandbox Code Playgroud) ggplots的数据:
set.seed(0)
library(ggplot2)
library(gridExtra)
c <- list()
for (k in 1:9) c[[k]] <- ggplot(data.frame(x=1:10,y=rnorm(10)),aes(x=x,y=y))+geom_line()
grid.arrange (c[[1]],c[[2]],c[[3]],c[[4]],c[[5]]
,c[[6]],c[[7]],c[[8]],c[[9]],ncol=3, nrow=3, widths = c(4,4,4) ,heights = c(4,4,4))
Run Code Online (Sandbox Code Playgroud)
我想要每一行和每一列的标题。
输出的形状如下所示:
CTitle 1 CTitle 2 CTitle 3
RTitle1 plot1 plot2 plot3
RTitle2 plot4 plot5 plot6
RTitle3 plot7 plot8 plot9
Run Code Online (Sandbox Code Playgroud) 我有以下数据集:
set.seed(6)
df <- data.frame(a=floor(runif(100)*5),b=floor(runif(100)*4),c=floor(runif(100)*3))
Run Code Online (Sandbox Code Playgroud)
我想为每个变量生成汇总freq表并将它们存储在一个数据集中.例如.
outexample <- rbind(table(df$a),c(table(df$b),0),c(table(df$c),0,0))
rownames(outexample) <- letters[1:3]
outexample
0 1 2 3 4
a 19 18 20 18 25
b 30 23 19 28 0
c 28 33 39 0 0
Run Code Online (Sandbox Code Playgroud)
每个变量中有数百个变量和未知数量的类.有没有更好的方式来做到这一点?
我在R中需要一个长序数向量。作为我想要的简单例子:
OS <- c("First","Second","Third")
Run Code Online (Sandbox Code Playgroud)
是否有这样的内置向量?
我有以下数据框
df <- data.frame(total=100,a=5,b=5)
Run Code Online (Sandbox Code Playgroud)
我想对每一列应用相同的功能。例如,通过划分第一列来划分所有列
df <- df/df$total
Run Code Online (Sandbox Code Playgroud)
但是,我想用 mutate_all 来做
我的代码是这样的
df <- data.frame(total=100,a=5,b=5) %>% mutate_all(list(~./total))
Run Code Online (Sandbox Code Playgroud)
这没有给我想要的输出。我究竟做错了什么?
df <- data.frame(id = 1:10, key = 1:10)
replace_key <- c(2,5)
replace_id <- c(9,3)
Run Code Online (Sandbox Code Playgroud)
我想通过replace_id将键值替换为replace_key中的值
所需效果:
id key
1 1
2 2
3 5
4 4
5 5
6 6
7 7
8 8
9 2
10 10
Run Code Online (Sandbox Code Playgroud)