我有一个数据框如下.最初只有两列/变量 - "Timestamp"(包含日期和时间)和"Actor".我将"时间戳"变量分解为"日期"和"时间",然后将"时间进一步分解为"小时"和"分钟".然后给出以下结构
dataf<-structure(list(hours = structure(c(3L, 4L, 4L, 3L, 3L, 3L, 6L,
6L, 6L, 6L, 6L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 1L, 1L, 2L, 2L), .Label = c("9",
"12", "14", "15", "16", "17"), class = "factor"), mins = structure(c(17L,
1L, 2L, 14L, 15L, 16L, 3L, 4L, 6L, 6L, 7L, 9L, 9L, 13L, 13L,
10L, 11L, 12L, 2L, 5L, 8L, 8L), .Label = c("00", "04", "08",
"09", "10", "12", "13", "18", "19", "20", "21", "22", "27", "39",
"51", "52", …Run Code Online (Sandbox Code Playgroud) 假设我有两个向量:
a <- c("george", "harry", "harry", "chris", "steve", "steve", "steve", "harry")
b <- c("harry", "steve", "chris", "harry", "harry", "george", "chris", "george")
Run Code Online (Sandbox Code Playgroud)
我想要做的是将第一对、第二对等粘贴在一起......但是,我想按字母顺序粘贴每对的两个元素。在上面的例子中,前 2 对已经按字母顺序排列,但第三对 'harry' 和 'chris' 不是。我想为这对返回“克里斯哈里”。
我已经在两步过程中找出了如何做到这一点,但想知道是否有一种快速的方法(单行方式)来做到这一点,只需使用paste?
我的解决方案:
x <- apply(mapply(c, a, b, USE.NAMES = FALSE), 2, sort)
paste(x[1,], x[2,])
Run Code Online (Sandbox Code Playgroud)
它按字母顺序给出了对……但是有 1 行方式吗?
[1] "george harry" "harry steve" "chris harry" "chris harry" "harry steve" "george steve" "chris steve" "george harry"
Run Code Online (Sandbox Code Playgroud) 我想知道我是否遗漏了一些微不足道的东西:
在对包含NA的此类矢量进行排名时,有四种选择如何处理NA:
x<-c(5, NA, 3, NA, 6, 9, 10, NA, 5, 7, 12)
rank(x, na.last=T)
# [1] 2.5 9.0 1.0 10.0 4.0 6.0 7.0 11.0 2.5 5.0 8.0
rank(x, na.last=F)
# [1] 5.5 1.0 4.0 2.0 7.0 9.0 10.0 3.0 5.5 8.0 11.0
rank(x, na.last=NA)
# [1] 2.5 1.0 4.0 6.0 7.0 2.5 5.0 8.0
rank(x, na.last="keep")
# [1] 2.5 NA 1.0 NA 4.0 6.0 7.0 NA 2.5 5.0 8.0
Run Code Online (Sandbox Code Playgroud)
我希望保持和排名NAs.为了我的目的,他们应该平等和最后排名.在这种情况下ties.method,要使用的是默认的"平均值".我正在寻找这个结果:
# [1] 2.5 10.0 1.0 10.0 4.0 …Run Code Online (Sandbox Code Playgroud) 这是一个小例子.在我的大数据集中,我有多年的数据,每组(div)的观察数量并不总是相等.
示例数据:
set.seed(1)
df<-data.frame(
year = 2014,
id = sample(LETTERS[1:26], 12),
div = rep(c("1", "2a", "2b"), each=4),
pts = c(9,7,9,3,7,5,3,7,2,7,7,1),
x = c(10,12,11,7,7,5,4,12,4,6,7,2)
)
Run Code Online (Sandbox Code Playgroud)
DF
# year id div pts x
#1 2014 G 1 9 10
#2 2014 J 1 7 12
#3 2014 N 1 9 11
#4 2014 U 1 3 7
#5 2014 E 2a 7 7
#6 2014 S 2a 5 5
#7 2014 W 2a 3 4
#8 2014 M 2a 7 12
#9 …Run Code Online (Sandbox Code Playgroud) 假设我有一个随机数向量,可以将它们从低到高排序:
set.seed(1)
x <- runif(20)
v <- x[order(x)]
Run Code Online (Sandbox Code Playgroud)
现在,说我想订购它们,但有一定程度的噪音。
我可以像这样随意移动元素:
z <-sample(1:20,2)
replace(v, z, v[rev(z)])
Run Code Online (Sandbox Code Playgroud)
但这不一定会移动紧密相关的值。我可能同样会随机地将第1和第20值切换为第5和第6值。我希望对切换有一些控制,因此我可以切换更紧密相关的值。
理想情况下,我将能够对向量进行重新排序以使其具有特定的Spearman相关性。说而不是完美排序时,排名的Spearman相关性为1,是否有办法重新排序相同的数字向量,例如Spearman相关性为0.5?
我正在尝试在 RMarkdown 中运行 Julia 块。我正在使用该包JuliaCall。以下是我已完成的步骤:
JuliaCalljulia_setup(JULIA_HOME = "C:/Users/James/Documents/Julia 1.5.1/bin")julia <- julia_setup()这是我的 RMarkdown 文件的最小示例:
---
title: "julia_eg"
author: "James"
date: "9/23/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
this is a julia example
```{julia}
a = sqrt(17)
a
```
Run Code Online (Sandbox Code Playgroud)
当我尝试编织这个时,它告诉我找不到 Julia - 我收到此错误:
Error in julia_locate(JULIA_HOME) : Can not find the Julia installation in the default installation path 'C:\Users\James\AppData\Local' Calls: <Anonymous> ... withVisible -> eval -> julia_setup -> julia_locate …
我正在使用 Pluto.jl 笔记本。我想使用 ggplot2 R 库来绘制一些图。
按照这个示例,如果我在 Julia REPL 中运行以下代码,那么我可以获得 ggplot2 图形输出。
using RCall
@rlibrary ggplot2
using DataFrames
df = DataFrame(v = [3,4,5], w = [5,6,7], x = [1,2,3], y = [4,5,6], z = [1,1,2])
ggplot(df, aes(x=:x,y=:y)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
现在,当我在 pluto.jl 笔记本中使用相同的代码(每行都是一个单独的单元格)时,我收到以下错误消息:
有没有办法让 ggplot2 图像出现在 pluto 笔记本中?
同样,如果我只是进入 ggplot()一个单元格,我会得到相同的错误,但是ggplot not defined。
这是这个问题的后续内容: Duplicating Observations of a dataframe, but also Replace Specific Variable Values in R
我尝试尽可能简洁地写,同时提供所有必要的信息。在当前示例中,我有一个如下所示的 df:
df<-data.frame(alpha=c(1, "3, 4", "2, 4, 5", 2, 1, 3, "1, 2", "1, 2, 3"),
beta=c("2, 4", "3, 4", 1, 3, 3, "1, 4", "1, 2", "1, 2, 3"),
color=c("red", "yellow"))
# alpha beta color
#1 1 2, 4 red
#2 3, 4 3, 4 yellow
#3 2, 4, 5 1 red
#4 2 3 yellow
#5 1 3 red
#6 3 1, 4 yellow …Run Code Online (Sandbox Code Playgroud) 示例数据(从formattablegithub docs 修改):
df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(48, 47, 40, 28, 29, 29, 27, 27, 31, 30),
test1_score = c(18.9, 19.5, 19.6, 12.9, 11.1, 7.3, 4.3, 3.9, 2.5, 1.6),
test2_score = c(9.1, 9.1, 9.2, 11.1, 13.9, 14.5, 19.2, 19.3, 19.1, 18.8),
stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)
您可以使用这样的额外颜色格式制作漂亮的表格:
library(formattable)
formattable(df, list(
age = color_tile("white", "orange"),
test1_score = color_bar("pink", 0.2),
test2_score = color_bar("pink", 0.2)
))
Run Code Online (Sandbox Code Playgroud)
看起来像这样: …
这是一个奇怪的请求 - 但是否有可能 1)从 RStudio 中终止 R 会话但保持 RStudio 打开?...然后 2) 从 RStudio 中开始一个新的 R 会话?