r

How to make multi-column Layout in R Markdown when rendering PDF?

こ雲淡風輕ζ 提交于 2021-02-10 06:36:20
问题 This is a great answer on how to do a 2-column layout in R-Markdown when rendering PDF. Basically the answer is 'add the following to the header': --- output: pdf_document: classoption: twocolumn --- But how do I make it three columns or more ? 回答1: To generate a html file with multiple columns, you could use the CSS grid layout: --- output: html_document --- :::: {style="display: grid; grid-template-columns: 20% 50% 20%; grid-column-gap: 5%; "} ::: {} contents... ::: ::: {} contents... ::: :

Repel geom label and text in ggplot. And ordering geom points based on size

江枫思渺然 提交于 2021-02-10 06:35:09
问题 I have 2 data frames such as these: df1 <- data.frame( party = c("Blue Party", "Red Party"), dim1 = c(0.03, -0.04), dim2 = c(-0.05, 0.02), sz = c(34, 42) ) df2 <- data.frame( var = c("Economic", "Gov trust", "Inst trust", "Nationalism", "Religiosity"), dim1 = c(0.1, -0.5, 0, 0.6, 0.4), dim2 = c(0.1, 0.6, 0, 0, 0.3) ) I want to plot the parties from df1 as points defined by size and include arrows based on df2 on the same graph. I've used ggplot to do this: ggplot(df1, aes(x = dim1, y = dim2,

How do you control the formatting of digits when converting lists of numbers to character?

。_饼干妹妹 提交于 2021-02-10 06:33:46
问题 I have a nested list containing numbers, for example x <- list(1 + .Machine$double.eps, list(rnorm(2), list(rnorm(1)))) If I call as.character on this, all the numbers are given in fixed format, to 15 significant digits. as.character(x) ## [1] "1" ## [2] "list(c(0.654345721043012, 0.611306113713901), list(-0.278722330674071))" I'd like to be able to control how the numbers are formatted. At the very least, I'd like to be able to control how many significant figures are included. As a bonus,

How do you control the formatting of digits when converting lists of numbers to character?

偶尔善良 提交于 2021-02-10 06:31:57
问题 I have a nested list containing numbers, for example x <- list(1 + .Machine$double.eps, list(rnorm(2), list(rnorm(1)))) If I call as.character on this, all the numbers are given in fixed format, to 15 significant digits. as.character(x) ## [1] "1" ## [2] "list(c(0.654345721043012, 0.611306113713901), list(-0.278722330674071))" I'd like to be able to control how the numbers are formatted. At the very least, I'd like to be able to control how many significant figures are included. As a bonus,

R read_yaml() reads a vector as parameter

那年仲夏 提交于 2021-02-10 06:29:08
问题 I would like to read a .yaml file to get yaml parameters for a Rmarkdown report. Original I have a yaml header to define a vector. --- params: ids: !r c(2455, 2490) --- and it works, where params$ids is a vector. However, if I put ids: !r c(2455, 2490) into a report_params.yaml file, and read that yaml file by report_params <- yaml::read_yaml("report_params.yaml") now report_params$ids is a string 'c(2455, 2490)' . so what did I miss, and how should I fix this? 回答1: The YAML default handler

Difference (angle) between two bearings

蹲街弑〆低调 提交于 2021-02-10 06:27:23
问题 Using geosphere::bearing I can calculate the bearing of two lines, but is it possible to calculate the angle between the two bearings ? Of course you can try and subtract or sum up the bearings but in specific cases where one is negative and the other is positive this doesn't work. For example if the ber1 = - 175 and ber2 = 175 the angle between should be 10. Any suggestions ? 回答1: I am not sure of a ready-made package but in case you are interested in a solution then you can try angle_diff <

Adding logos/images to the side of a data table

十年热恋 提交于 2021-02-10 06:26:49
问题 I have searched for a way to create a table with logos/images as a column of a data table. I've attached an image of the sort of table I'd like. The data table was taken from an example using library(formattable) , and I pasted in logos on top of the 'id' column to show the sort of design I'm looking for. Ideally this would be neater and customizable (perhaps the entire table background in black with white/grey writing etc. Does anyone have any examples they can share? Code to create

How do you scrape items together so you don't lose the index?

天涯浪子 提交于 2021-02-10 06:20:11
问题 I am doing some basic webscraping with RVest and am getting results to return, however the data isnt lining up with each other. Meaning, I am getting the items but they are out of order from the site so the 2 data elements I am scraping cant be joined in a data.frame. library(rvest) library(tidyverse) base_url<- "https://www.uchealth.com/providers" loc <- read_html(base_url) %>% html_nodes('[class=locations]') %>% html_text() dept <- read_html(base_url) %>% html_nodes('[class=department last]

how to use group_by in a function in R

那年仲夏 提交于 2021-02-10 06:16:14
问题 I want to use group_by in a function, the following is my code, 1, 2 work well, so I create a function - 3, while it doesn't work in 4. I don't known how to address this problem, so ask for a help. # 1 generate variables and dataframe x <- rnorm(100) y <- rep(c("A", "B"), 50) df <- data.frame(y, x) # 2 group by y df %>% group_by(y) %>% summarise(n = n(), mean = mean(x), sd = sd(x)) # 3 create function group <- function(df, var1, var2){ df %>% group_by(var1) %>% summarise(n = n(), mean = mean

Create new column in dataframe using if {} else {} in R

霸气de小男生 提交于 2021-02-10 06:15:20
问题 I'm trying to add a conditional column to a dataframe, but not getting the results I'm expecting. I have a dataframe with values recorded for the column "steps" across 5-minute intervals over various days. I'm trying to impute missing values in the 'steps' column by using the mean number of steps for a given 5-minute interval on the days that do have measurements. n.b. I tried using the MICE package for this but it just crashed my computer so I opted for a more manual workaround. As an