小编ell*_*iot的帖子

使用dplyr和select_if从数据框中删除列表列

是否有可能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)

r list dplyr

5
推荐指数
1
解决办法
74
查看次数

如何控制ggraph中的节点颜色?

我有一个网络图,我想为边着色以匹配它们各自的节点。这在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)

r colors nodes ggraph

5
推荐指数
1
解决办法
5055
查看次数

删除 ggraph 中未使用的节点

我有以下数据框。

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)

r ggraph

5
推荐指数
0
解决办法
170
查看次数

在 igraph 中绘制网络时,R 绘图边距太大

我已经搜索过,但还没有找到可以开始工作的解决方案。我没有任何可复制的数据,但我有下图来说明我的问题:我的图太小了。对我的问题可能是什么有什么想法吗?下图大约有一半的区域是空白。

在此输入图像描述

r igraph

4
推荐指数
1
解决办法
2698
查看次数

使用if_else,如果条件为false,则无法返回用作条件的列

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)

if-statement r dplyr

3
推荐指数
1
解决办法
1125
查看次数

检测两个字符串向量之间的差异

我有一个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)

我宁愿发现使用的元素的溶液tidyversestringr可能的话,但我还是要像我得到它.

string r stringr tidyverse

3
推荐指数
1
解决办法
953
查看次数

如何在dplyr管道中使用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 (.)

谢谢。

r stringr dplyr

3
推荐指数
1
解决办法
1737
查看次数

在 R sf 的分组列上创建新几何

我想创建一个新的 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)

在此处输入图片说明

r mutate r-sf

3
推荐指数
1
解决办法
605
查看次数

将数据加载到 R 的最佳文件类型(速度方面)?

我正在运行一些分析,其中我得到了很多介于 2-3G 之间的数据集。现在,我将其保存为.RData文件类型。然后,稍后我加载这些文件以继续工作,这需要一些时间来加载。我的问题是:将这些文件保存然后加载为 .csv 会更快。是data.table读取 .csv 文件的最快包吗?我想我正在寻找 R 中的最佳工作流程。

performance load r

2
推荐指数
1
解决办法
2004
查看次数

更改剪切功能中的标签符号样式?

我正在使用一组ggplot2函数在 R 中创建一些分箱数据。符号样式是“(a,b]”。我知道可以使用预先指定的标签。但我喜欢使用返回的数字标签的想法默认提供。但是,我想将符号样式更改为“a - b”之类的样式。这可能吗?

cut r notation ggplot2

2
推荐指数
1
解决办法
71
查看次数

标签 统计

r ×10

dplyr ×3

ggraph ×2

stringr ×2

colors ×1

cut ×1

ggplot2 ×1

if-statement ×1

igraph ×1

list ×1

load ×1

mutate ×1

nodes ×1

notation ×1

performance ×1

r-sf ×1

string ×1

tidyverse ×1