我有几个列表列的小标题,我只想嵌套其中的一个。
例
library(dplyr)
library(purrr)
library(tidyr)
library(stringr)
iris %>%
group_by(Species) %>%
nest() %>%
mutate(sum_data = map(data,
~.x %>%
summarize_all(mean) %>%
rename_all(funs(str_c("Mean.", .)))))
# A tibble: 3 x 3
# Species data sum_data
# <fctr> <list> <list>
# 1 setosa <tibble [50 x 4]> <tibble [1 x 4]>
# 2 versicolor <tibble [50 x 4]> <tibble [1 x 4]>
# 3 virginica <tibble [50 x 4]> <tibble [1 x 4]>
Run Code Online (Sandbox Code Playgroud)
现在,我想保留嵌套的data列,但sum_data不嵌套该列,而无需在其中专门指定每个列名sum_data,也无需取消嵌套整个数据集,然后重新嵌套数据列。
期望的结果
# A tibble: 3 …Run Code Online (Sandbox Code Playgroud) 我想在函数调用中提取列名mutate_if.有了这个,我想在另一个表中查找一个值,并用查找值填充缺失值.我尝试使用quosure语法,但它无法正常工作.是否有可能直接提取列名?
样本数据
df <- structure(list(x = 1:10,
y = c(1L, 2L, 3L, NA, 1L, 2L, 3L, NA, 1L, 2L),
z = c(NA, 2L, 3L, NA, NA, 2L, 3L, NA, NA, 2L),
a = c("a", "b", "c", "d", "e", "a", "b", "c", "d", "e")),
.Names = c("x", "y", "z", "a"),
row.names = c(NA, -10L),
class = c("tbl_df", "tbl", "data.frame"))
df_lookup <- tibble(x = 0L, y = 5L, z = 8L)
Run Code Online (Sandbox Code Playgroud)
不工作
它不能直接以某种方式提取名称.
df %>%
mutate_if(is.numeric, funs({
x …Run Code Online (Sandbox Code Playgroud) 我想small使用 总结每个不同 video.id 的数据dplyr。
small %>%
group_by(Video.ID) %>%
summarise(sumr = sum(Partner.Revenue),
len = mean(Video.Duration..sec.),
cat = mean(Category))
Run Code Online (Sandbox Code Playgroud)
Mean(Category) 显然是错误的方法。如何获取它只是为了使用重复多次的值(一个 video.id 始终具有相同的类别,无论它在数据帧中出现的频率如何)。
我的数据框如下所示:
small
# A tibble: 6 x 7
X1 X1_1 Video.ID Video.Duration..sec. Category Owned.Views Partner.Revenue
<int> <int> <chr> <int> <chr> <int> <dbl>
1 1 1 ---0zh9uzSE 1184 gadgets 6 0
2 2 2 ---0zh9uzSE 1184 gadgets 6 0
3 3 3 ---0zh9uzSE 1184 gadgets 2 0
4 4 4 ---0zh9uzSE 1184 gadgets 1 0
5 …Run Code Online (Sandbox Code Playgroud) 我想在数据框中表示表数据,其中表具有跨越多行的多个标题,如下例所示。

使用数据框,我只能设置一行标题或列名称,如下所示
colnames(df) <- c("col1", "col2", ...)
Run Code Online (Sandbox Code Playgroud)
有没有办法为数据框提供多个标题或列名称,或者是否有更好的数据结构或包可用于表示此类数据结构?
正在探索避免for循环的方法,而是使用any()函数来实现一个函数,当传递的参数为prime和false时,它给出true.
这是我有的:
prime2 <- function(n) {
rangeOfNumbers <- range(2:(n-1))
if(any(n%%rangeOfNumbers == 0)){
return(FALSE)
}
else return(TRUE)
}
Run Code Online (Sandbox Code Playgroud)
看起来很直接,但prime(55)给出TRUE而不是假.
我究竟做错了什么?
当我要创建散点图矩阵时,出现错误
grid.Call.graphics(C_downviewport,name $ name,strict)中的错误:未找到视口'plot_01.panel.1.1.off.vp'”。
我该如何解决?
varNum <- function(x){
val <- 1:ncol(x)
names(val) <- colnames(x)
return(val)
}
varNum(house)
Bedroom SquareFeet Followers VisitingTime TotalPrice UnitPrice
1 2 3 4 5 6
District Location
7 8
house1 <- house[,c(7,1:6)]
offDiag <- function(x,y,...){
panel.grid(h = -1,v = -1,...)
panel.hexbinplot(x,y,xbins = 15,...,border = gray(.7),
trans = function(x)x^.5)
# panel.loess(x , y, ..., lwd=2,col='red')
}
onDiag <- function(x, ...){
yrng <- current.panel.limits()$ylim
d <- density(x, na.rm = TRUE)
d$y <- with(d, yrng[1] + 0.95 * diff(yrng) …Run Code Online (Sandbox Code Playgroud) 我希望您能想到一种更优雅的方式来计算出前几天发生的事件数量。我的代码(如下)可以工作,但是不是很好,也不是可伸缩的。我正在尝试到达底部的表(desired_table)。有什么想法吗?
我想以比这更优雅的方式来计算前几天的事件总数。
require(data.table)
# simulating an example data.table
date <- c("2000-01-01", "2000-01-04", "2000-01-05", "2000-01-06", "2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", "2000-01-05", "2000-01-06" , "2000-01-01", "2000-01-04", "2000-01-05", "2000-01-06", "2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", "2000-01-05", "2000-01-06")
cohort <- c("a", "b", "c")
zz <- data.table(DATE = date, COHORT = cohort)
zz$DATE <- as.Date(zz$DATE) # making sure the date is in the correct format
# adding on some other date fields so we can summarise by these days as well
zz$d1 <- zz$DATE +1 # will become …Run Code Online (Sandbox Code Playgroud) 我是R的新手,并试图根据车牌号和日期时间将2组数据连接起来。
数据集1
LicensePlate DateTime
XLP1234P 09-JUN-18 02.52.40.144000000 PM
XLP2345P 18-JUL-18 11.22.46.855000000 AM
XLP3456P 18-JUL-18 11.22.46.856000000 AM
XLP4567P 18-JUL-18 11.22.46.856000000 AM
XLP5678P 18-JUL-18 11.22.46.857000000 AM
XLP6789P 18-JUL-18 11.22.46.858000000 AM
Run Code Online (Sandbox Code Playgroud)
数据集2
LicensePlate DateTime
XLP1234P 09-JUN-18 02.55.40.144000000 PM
XLP2345P 18-JUL-18 11.30.46.855000000 AM
Run Code Online (Sandbox Code Playgroud)
基本上,数据集是由2套不同的设备记录的,因此会略有时间差。我想在可接受的10分钟时差基础上加入车牌的第一组。
left_join 可以按列值合并数据,但是如何设置条件以使datetime在合适的范围内?