小编SCD*_*DCE的帖子

如何更改现已弃用的dplyr :: funs()(其中包含ifelse参数)?

很基本,但我认为我不太了解这种变化:

library(dplyr)
library(lubridate)

Lab_import_sql <- Lab_import %>%
    select_if(~sum(!is.na(.)) > 0) %>%
    mutate_if(is.factor, as.character) %>%
    mutate_if(is.character, funs(ifelse(is.character(.), trimws(.),.))) %>%
    mutate_at(.vars = Lab_import %>% select_if(grepl("'",.)) %>% colnames(),
                 .funs = gsub,
                 pattern = "'",
                 replacement = "''") %>%
    mutate_if(is.character, funs(ifelse(is.character(.), paste0("'", ., "'"),.))) %>%
    mutate_if(is.Date, funs(ifelse(is.Date(.), paste0("'", ., "'"),.)))
Run Code Online (Sandbox Code Playgroud)

编辑:

感谢大家的投入,这里是可复制的代码和我的解决方案:

library(dplyr)
library(lubridate)

import <- data.frame(Test_Name = "Fir'st Last", 
                     Test_Date = "2019-01-01", 
                     Test_Number = 10)

import_sql <-import %>%
  select_if(~!all(is.na(.))) %>%
  mutate_if(is.factor, as.character) %>%
  mutate_if(is.character, trimws) %>%
  mutate_if(is.character, list(~gsub("'", "''",.))) %>% 
  mutate_if(is.character, list(~paste0("'", ., …
Run Code Online (Sandbox Code Playgroud)

r dplyr

9
推荐指数
1
解决办法
2887
查看次数

如何在 factoextra 的 fviz_pca_var 中设置数据标签的字体大小

当我向期刊提交论文时,期刊要求我将字体大小设置为7.5,但是当我使用以下代码时,只有轴文本发生了变化,标签大小保持不变,

    library(ggplot2)
    library(FactoMineR)
    library(factoextra)

    irispca <- PCA(iris,quali.sup = 5)
    fviz_pca_var(irispca)+
   theme(text = element_text(size = 7.5),
         axis.title = element_text(size = 7.5),
         axis.text = element_text(size = 7.5)
         )
Run Code Online (Sandbox Code Playgroud)

任何建议都可以告诉我应该使用什么代码来更改标签大小?

在此处输入图片说明

r ggplot2

6
推荐指数
1
解决办法
7068
查看次数

如何在ggplot的geom_label中设置标准标签大小?

geom_label用来绘制文本。默认情况下,标签(框)的宽度取决于文本的宽度。但是,我想要统一的标签尺寸。也就是说,我想要文本后面矩形的固定大小,而不管字符串的长度。

我当前标签的示例,根据字符串长度具有不同的大小:

1]

我如何制作看起来像这样的标签:

2

我查看了geom_label github,它看起来不太好,我在想也许是某种修改unit() 的方法,但我什么也做不了。

对于一些可重现的代码:

library(ggplot2)
ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) +
geom_text(check_overlap = TRUE) +
geom_label(aes(fill= factor(cyl)))
Run Code Online (Sandbox Code Playgroud)

你将如何使所有标签的尺寸相同?

plot r ggplot2

4
推荐指数
2
解决办法
4509
查看次数

如何让一个函数在expression()内正常执行?

我正在尝试将一些标签部分斜体化,但仍然具有sum()功能,但我不知道该怎么做:

library(ggplot2)
library(reshape2)

iris_mean <- aggregate(iris[,1:4], by=list(Species=iris$Species), FUN=mean) 
iris_sd <- aggregate(iris[,1:4], by=list(Species=iris$Species), FUN=sd) 
df_mean <- melt(iris_mean, id.vars=c("Species"), variable.name = "Samples", value.name="Values")
df_sd <- melt(iris_sd, id.vars=c("Species"), variable.name = "Samples", value.name="Values")
limits <- aes(ymax = df_mean[,"Values"] + df_sd[,"Values"], ymin=df_mean[,"Values"] - df_sd[,"Values"])
df_mean$Species <- factor(df_mean$Species, levels=unique(df_mean$Species), ordered=TRUE)

ggplot(df_mean, aes(Samples, Values, fill = Species)) + 
  geom_bar(position="dodge", stat="identity") + coord_flip() + 
  theme(axis.text.y=element_text(angle=0, hjust=1)) +
  theme_minimal() +
  theme(
    legend.position = "bottom",
    plot.margin = unit(c(1,1,.5,0),"cm"),
    legend.key.size = unit(.5, "lines"),
    axis.text.y = element_text(face = "italic")
  ) …
Run Code Online (Sandbox Code Playgroud)

expression r ggplot2

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

按不同的降序排列数字

相当简单的问题我似乎无法想出一个优雅的解决方案。

我想通过不同的降序排列一列数据:

library(dplyr)
test <- data.frame(ID=c(19000,19001,19002,1,2))

test %>% 
  arrange(desc(ID)) %>%
  mutate(ID = formatC(ID,width=5,format="d",flag="0"))

     ID
1 19002
2 19001
3 19000
4 00002
5 00001
Run Code Online (Sandbox Code Playgroud)

我想要:

     ID
1 00002
2 00001
3 19002
4 19001
5 19000
Run Code Online (Sandbox Code Playgroud)

这是用于管道,因此将添加更多 ID,例如 00003、00004....

这是我想出的东西:

test %>% 
  mutate(ID = formatC(ID,width=5,format="d",flag="0")) %>% 
  group_by(group=substr(ID,1,1)) %>% 
  arrange(desc(ID)) %>% 
  arrange(group) %>% 
  ungroup() %>% 
  select(ID)
Run Code Online (Sandbox Code Playgroud)

还有比这更好的吗?

编辑 -

library(microbenchmark)

test <- data.frame(ID=c(1:29999))

microbenchmark(group = test %>% 
                 mutate(ID = formatC(ID,width=5,format="d",flag="0"),
                        group = substr(ID,1,1)) %>% 
                 arrange(group, desc(ID)) %>% 
                 select(ID),

               mod = test …
Run Code Online (Sandbox Code Playgroud)

r dplyr

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

bind_rows(),列不能从整数转换为字符错误

我看过这个答案:错误 in bind_rows_(x, .id) : Column can't be convert from factor to numeric but I can't mutate_all()a list。

library(rvest)
library(dplyr)
library(tidyr)

fips <- read_html("https://www.census.gov/geo/reference/ansi_statetables.html") %>% 
  html_nodes("table") %>% 
  html_table() %>% 
  bind_rows(.[[1]][1:3] %>% 
          transmute(name = `Name`,
                    fips = as.character(`FIPS State Numeric Code`),
                    abb = `Official USPS Code`),
        filter(.[[2]][1:3], !grepl("Status",c(`Area Name`))) %>% 
          transmute(name = `Area Name`,
                    fips = as.character(`FIPS State Numeric Code`),
                    abb = `Official USPS Code`))

Error in bind_rows_(list_or_dots(...), id = NULL) : 
  Column `FIPS State Numeric Code` can't be …
Run Code Online (Sandbox Code Playgroud)

r dplyr

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

将多个列粘贴到单个列中,但删除任何NA,空白或重复值

我的数据看起来像这样:

dat <- data.frame(SOURCES1 = c("123 Name, 123 Rd, City, State", 
                               "354 Name, 354 Rd, City, State",
                               NA,"",""),
                  SOURCES2 = c("","",
                               "321 Name, 321 Rd, City, State", 
                               "678 Name, 678 Rd, City, State",
                               ""),
                  SOURCES3 = c("","",NA,
                               "678 Name, 678 Rd, City, State", 
                               NA),
                  SOURCES4 = c("","","",NA,NA),
                  SOURCES5 = c("","","",NA,NA))
Run Code Online (Sandbox Code Playgroud)

我正在寻找一个看起来像这样的列:

"123 Name, 123 Rd, City, State"
"354 Name, 354 Rd, City, State"
"321 Name, 321 Rd, City, State"
"678 Name, 678 Rd, City, State"
NA
Run Code Online (Sandbox Code Playgroud)

r dplyr

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

标准化电话号码数据

我正在寻找一个更优雅的解决方案:

phone_number <- function(x) {
x <- gsub("[\\() -]", "", x) 
x <- gsub("^(.{3})(.{3})(.*)","\\1-\\2-\\3", x, perl = TRUE)
}
Run Code Online (Sandbox Code Playgroud)

这将采用这样的数据:

(123) 123-1234
123-123-1234
123 123-1234
Run Code Online (Sandbox Code Playgroud)

并产生这个:

123-123-1234
123-123-1234
123-123-1234
Run Code Online (Sandbox Code Playgroud)

regex r

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

标签 统计

r ×8

dplyr ×4

ggplot2 ×3

expression ×1

plot ×1

regex ×1