是否有可能dpyr select像删除单个列一样删除数据框中的所有列表列?
df <- tibble(
a = LETTERS[1:5],
b = 1:5,
c = list('bob', 'cratchit', 'rules!','and', 'tiny tim too"')
)
df %>%
select_if(-is.list)
Error in -is.list : invalid argument to unary operator
Run Code Online (Sandbox Code Playgroud)
这似乎是一个可行的解决方法,但想知道是否可以使用完成select_if。
df %>%
select(-which(map(df,class) == 'list'))
Run Code Online (Sandbox Code Playgroud) 我有一个网络图,我想为边着色以匹配它们各自的节点。这在igraph情节中相当直接,但我更喜欢这样做,ggraph因为我喜欢包提供的其他美学。
似乎对ggraph; 中的节点颜色几乎没有控制。而边缘颜色被广泛覆盖。
我的问题是:如何将我的边与使用自定义函数着色的节点进行匹配,以便“离开”节点的每条边的颜色与其节点相同。这应该有助于人们更轻松地通过网络跟踪流量。一个更普遍的问题是:ggraph 如何在美学论证之外分配颜色。我的问题类似于我之前在这里问过的另一个问题,但反过来(将边与节点匹配),在这里找到。
这是一个可重现的示例:
library(tidyverse)
library(igraph)
library(ggraph)
library(tidygraph)
library(RColorBrewer)
## the custom function using Color Brewer
cols_f <- colorRampPalette(RColorBrewer::brewer.pal(11, 'Spectral'))
## make the graph
g <- erdos.renyi.game(50, .1)
# provide some names
V(g)$name <- 1:vcount(g)
#plot using ggraph
g %>%
as_tbl_graph() %>%
activate(nodes) %>%
mutate(degree = centrality_degree()) %>%
ggraph()+
geom_edge_fan(aes(color = as.factor(from),
alpha = ..index..),
show.legend = F)+
geom_node_point(aes(size = degree),
color = cols_f(vcount(g)), # custom function for node color …Run Code Online (Sandbox Code Playgroud) 我有以下数据框。
library(tidyverse)
library(ggraph)
library(tidygraph)
df <- structure(list(from = c(3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L,
2L, 1L, 3L, 1L, 3L, 3L, 3L, 1L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L,
1L, 3L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 1L, 3L, 1L, 1L, 3L, 1L, 2L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 1L, 2L, 1L,
3L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 3L, 1L, 1L, …Run Code Online (Sandbox Code Playgroud) 我flag在数据集中创建了一个不同作业描述的变量.我想将每个flag==true变量放到另一个变量的同一行位置.
我已经尝试了ifelse,if_else;但我能够得到的是true条件和NAs 的更改值列表.
以下是一个可重现的示例diamonds:
mydata <- diamonds[1:10,c(3,4)]
mydata$position <- c('flag','cathy')
mydata
mydata$new.vary <- mydata %>% if_else(color=='E',position,color)
Run Code Online (Sandbox Code Playgroud) 我有一个data_frame看起来像这样的东西.
df <- data_frame(name = c('john','bill','amy'),
name.2 = c('johhn','ball','ammy'))
df
# A tibble: 3 x 2
name name.2
<chr> <chr>
1 john johhn
2 bill ball
3 amy ammy
Run Code Online (Sandbox Code Playgroud)
我想添加一个列,显示两个名称(.2)列之间的差异.像这样:
df %>%
mutate(diff = c('h','a','m'))
# A tibble: 3 x 3
name name.2 diff
<chr> <chr> <chr>
1 john johhn h
2 bill ball a
3 amy ammy m
Run Code Online (Sandbox Code Playgroud)
我宁愿发现使用的元素的溶液tidyverse和stringr可能的话,但我还是要像我得到它.
我在尝试编辑dplyr管道中的某些字符串的这段代码时遇到麻烦。这是一些数据,它引发以下错误。有任何想法吗?
data_frame(id = 1:5,
name = c('this and it pretty long is a',
'name is a',
'so and so and so and so and so',
'this is a',
'this is a variabel name'))
%>%
str_trunc(.,
string = .$name,
width = 10,
side='right',
ellipsis = '')
Run Code Online (Sandbox Code Playgroud)
给我这个错误:Error in str_trunc(., string = .$name, width = 10, side = "right", ellipsis = ". . . ") : unused argument (.)。
谢谢。
我想创建一个新的 shapefile 或一个新的几何变量,以允许我在 R 中绘制区域周围的边界。我使用sf和 映射与tmap. 基本上,我正在向sf对象添加字符向量,并希望将字符向量设为新的/首选映射边框。
这是我的方法的一个例子,它没有做我想要的。我不能说它有什么作用。
library(tidyverse)
library(sf)
library(tmap)
## use North Carolina example
nc = st_read(system.file("shape/nc.shp", package="sf"))
nc_new.region <- nc %>% ## add new region variable
mutate(new.region = sample(c('A', 'B', 'C'), nrow(.),replace = T))
nc_union <- nc_new.region %>%
group_by(new.region) %>% # group by the new character vector
mutate(new_geometry = st_union(geometry)) # union on the geometry variable
# map with tmap package
tm_shape(nc_union)+
tm_borders()
Run Code Online (Sandbox Code Playgroud)
我正在运行一些分析,其中我得到了很多介于 2-3G 之间的数据集。现在,我将其保存为.RData文件类型。然后,稍后我加载这些文件以继续工作,这需要一些时间来加载。我的问题是:将这些文件保存然后加载为 .csv 会更快。是data.table读取 .csv 文件的最快包吗?我想我正在寻找 R 中的最佳工作流程。
我正在使用一组ggplot2函数在 R 中创建一些分箱数据。符号样式是“(a,b]”。我知道可以使用预先指定的标签。但我喜欢使用返回的数字标签的想法默认提供。但是,我想将符号样式更改为“a - b”之类的样式。这可能吗?