如何使用 dplyr 将人口列乘以 -1(仅限性别“kvinnor”)?
\n以下是将人口列中的所有值相乘的代码:
\ndf %>% \n mutate(neg_kv = population*-1)\nRun Code Online (Sandbox Code Playgroud)\n# data\ndf <- tibble::tribble(\n ~region, ~marriage_status, ~age, ~gender, ~population, ~year,\n "Riket", "ogifta", 15, "m\xc3\xa4n", 56031, 1968,\n "Riket", "ogifta", 15, "kvinnor", 52959, 1968,\n "Riket", "ogifta", 16, "m\xc3\xa4n", 55917, 1968,\n "Riket", "ogifta", 16, "kvinnor", 52979, 1968,\n "Riket", "ogifta", 17, "m\xc3\xa4n", 55922, 1968,\n "Riket", "ogifta", 17, "kvinnor", 52050, 1968,\n "Riket", "ogifta", 18, "m\xc3\xa4n", 58681, 1968,\n "Riket", "ogifta", 18, "kvinnor", 51862, 1968,\n "Riket", "ogifta", 19, "m\xc3\xa4n", 60387, 1968,\n "Riket", "ogifta", …Run Code Online (Sandbox Code Playgroud) 我正在将一系列李克特量表问题绘制为堆积条形图。它们需要标记,但有些片段是两个小片段,并且会重叠,如下例所示(下面有示例代码)。是否有一种聪明的方法来仅标记足够大以适合标签的部分,或者交错显示标签的方式?
q1a<-rep(c("A", "B", "C", "D"), c(2, 5,45,45))
q1b<-rep(c("A", "B", "C", "D"), c(45,45,2, 5))
data<-data.frame(q1a,q1b)
data %>%
select(q1a:q1b) %>%
pivot_longer(cols = everything(), names_to = "Question") %>%
filter(!is.na(value)) %>%
count(Question, value) %>%
group_by(Question) %>%
mutate(Pct = n / sum(n)) %>%
ggplot(aes(fill = value, x = Pct, y = fct_rev(Question))) +
geom_bar(position = "fill", stat = "identity") +
geom_text(aes(label = paste0(sprintf("%1.0f", Pct * 100), "%")), position = position_stack(vjust = 0.5), size = 3) +
scale_fill_brewer(palette = "Blues") +
theme_bw() +
scale_x_continuous(labels = scales::percent) …Run Code Online (Sandbox Code Playgroud) 我有一个很长的标题,我想将其包含在 ggplot 的底部geom_boxplot。
目前,代码将一行写入标题,但缺少许多预期的标题。我如何将文字包裹在标题中?
我的代码是
p = ggplot(mpg, aes(class, hwy)) + geom_boxplot() +
labs(title="An example title", caption = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in …Run Code Online (Sandbox Code Playgroud) 假设我有这个向量:
letters[1:7]
[1] "a" "b" "c" "d" "e" "f" "g"
Run Code Online (Sandbox Code Playgroud)
我想将它分成一个不重叠的列表,长度增加1,并保留剩下的内容(例如子列表4应该有4个元素,但只剩下一个,我想保留那个) ),如下所示:
[[1]]
[1] "a"
[[2]]
[1] "b" "c"
[[3]]
[1] "d" "e" "f"
[[4]]
[1] "g"
Run Code Online (Sandbox Code Playgroud)
请告诉我解决这个问题的任何方向,谢谢!
假设我有一个字符串向量:
vec <- c("D", "A", "B", "A")
Run Code Online (Sandbox Code Playgroud)
我想在原始字符串之后立即插入修改后的字符串(在字符串末尾连接“_2”),而不进行排序。
预期输出:
[1] "D" "D_2" "A" "A_2" "B" "B_2" "A" "A_2"
Run Code Online (Sandbox Code Playgroud)
我正在寻找比我在下面分享的解决方案更简单的解决方案,我认为这可能不必要地过于复杂。
我有一个列表,代表单个出版物的研究信息领域。我想将列表合并到 data.frame 中,以便每个 2 位代码存储在“部门”列中,每个 4 位代码存储在“组”列中。当前两位数字共享时,部门和组应存储在同一行。我为这个不好的标题道歉。
my_list <- list(
list(id = "80067", name = "3403 Macromolecular and Materials Chemistry"),
list(id = "80011", name = "40 Engineering"),
list(id = "80005", name = "34 Chemical Sciences")
)
Run Code Online (Sandbox Code Playgroud)
期望的输出:
data.frame(division = c("40 Engineering", "34 Chemical Sciences"),
group = c(NA, "3403 Macromolecular and Materials Chemistry"))
Run Code Online (Sandbox Code Playgroud) 我以为我了解 map() 的工作原理,但显然我不明白。我有一个与工作目录中的真实文件相对应的文件名向量:
\nfile_names <- c("filename1", "filename2", "filename3")\nRun Code Online (Sandbox Code Playgroud)\n这有效:
\nlibrary(tidyverse)\n\ndata <- data_frame(filename = file_names) %>%\n mutate(content = map(file_names, read_tsv))\nRun Code Online (Sandbox Code Playgroud)\n但是当我想使用推荐的格式向 read_tsv 添加参数时map(\\\\(x) f(x, arg1, arg2))
data <- data_frame(filename = file_names) %>%\n mutate(content = map(\\(file_names) read_tsv(file_names, skip = 1, col_names = FALSE)))\nRun Code Online (Sandbox Code Playgroud)\n我收到错误:
\n\n\n错误
\nmutate():\n\xe2\x84\xb9 参数:content = map(function(file_names) read_tsv(file_names, skip = 1, col_names = FALSE))。\n由以下错误引起map():\n!.x必须是向量,而不是函数。
我尝试将 file_names 更改为 .x 等,但我不知道它是如何工作的。任何帮助表示赞赏。
\n