我想要:
across和case_when检查列 A1-A3 == 1我的数据框:
df <- tribble(
~ID, ~A1, ~A2, ~A3,
1, 0, 1, 1,
2, 0, 1, 1,
3, 1, 1, 1,
4, 1, 0, 1,
5, 0, 1, 0)
Run Code Online (Sandbox Code Playgroud)
期望输出:
# A tibble: 5 x 5
ID A1 A2 A3 New_Col
<dbl> <dbl> <dbl> <dbl> <chr>
1 1 0 1 1 A2 A3
2 2 0 1 1 A2 A3
3 3 1 1 1 A1 …Run Code Online (Sandbox Code Playgroud) 如何将组扩展到最大组的长度:
df <- structure(list(ID = c(1L, 1L, 2L, 3L, 3L, 3L), col1 = c("A",
"B", "O", "U", "L", "R")), class = "data.frame", row.names = c(NA,
-6L))
ID col1
1 A
1 B
2 O
3 U
3 L
3 R
Run Code Online (Sandbox Code Playgroud)
期望的输出:
1 A
1 B
NA NA
2 O
NA NA
NA NA
3 U
3 L
3 R
Run Code Online (Sandbox Code Playgroud) 这在某种程度上与此相关的问题:原则上我试着去了解如何rowwise操作与mutate多个列采用更然后像(1个功能mean(),sum(),min()等)的工作。
我已经了解到可以across完成这项工作而不是c_across。我已经学会了该功能mean()是将不同的功能min()以如下方式mean()不起作用在dataframes,我们需要将其更改到可以不公开或as.matrix做载体- >从Ronak沙阿了解到这里了解横行()和 c_across()
现在以我的实际情况为例:我能够完成这项任务,但我丢失了一个 column d。我怎样才能避免d这种设置中的柱子松动。
我的 df:
df <- structure(list(a = 1:5, b = 6:10, c = 11:15, d = c("a", "b",
"c", "d", "e"), e = 1:5), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
不工作:
df %>%
rowwise() %>%
mutate(across(a:e),
avg = mean(unlist(cur_data()), na.rm = TRUE),
min = …Run Code Online (Sandbox Code Playgroud) 这是如何将行添加到仅修改某些列的数据帧的后续问题。
解决这个问题后,我想将 stefan 提供的解决方案应用到更大的数据框group_by:
我的数据框:
df <- structure(list(test_id = c(1, 1, 1, 1, 1, 1, 1, 1), test_nr = c(1,
1, 1, 1, 2, 2, 2, 2), region = c("A", "B", "C", "D", "A", "B",
"C", "D"), test_value = c(3, 1, 1, 2, 4, 2, 4, 1)), class = "data.frame", row.names = c(NA,
-8L))
test_id test_nr region test_value
1 1 1 A 3
2 1 1 B 1
3 1 1 C 1
4 1 1 D …Run Code Online (Sandbox Code Playgroud) 我经常使用mapshot来发送交互式地图数据,但最近,虽然我可以用mapview制作我想要的地图,但我无法保存它们。
例子:
map<- mapview(mapdata, zcol = "columnofinterest", burst = TRUE)
mapshot(map, url = paste0(getwd(), "/whatIwanttocallmymap.html"))
File whatIwanttocallmymap_files/PopupTable-0.0.1/popup.css not found in resource path
Error: pandoc document conversion failed with error 99
Run Code Online (Sandbox Code Playgroud)
恐怕我在获取包裹的方式上搞砸了。带有包名称的文件夹出现在我设置为 wd 的区域中,而不是出现在我的 R 库中
感谢您提供的任何帮助/建议
我想在~incase_when函数之后放置一个条件项。我的例子:
df:
df <- structure(list(x = c("a", "a", "a", "b", "b", "b", "c", "c",
"c", "a", "a", "a"), y = 1:12), class = "data.frame", row.names = c(NA,
-12L))
Run Code Online (Sandbox Code Playgroud)
不工作的代码:
library(dplyr)
df %>%
group_by(x) %>%
mutate(y = case_when(x=="b" ~ cumsum(y),
TRUE ~ y)) %>%
mutate(y = case_when(x=="a" ~ "what I want: last value of group "b" in column y",
TRUE ~ y))
Run Code Online (Sandbox Code Playgroud)
用一句话来说:
group_by xcumsum组by我有这个小玩意:
# A tibble: 2 x 8
a.x b.x c.x d.x a.y b.y c.y d.y
<int> <int> <int> <int> <int> <int> <int> <int>
1 13 13 12 11 7 1 4 2
2 17 11 0 0 16 2 0 0
Run Code Online (Sandbox Code Playgroud)
df <- structure(list(a.x = c(13L, 17L), b.x = c(13L, 11L), c.x = c(12L,
0L), d.x = c(11L, 0L), a.y = c(7L, 16L), b.y = 1:2, c.y = c(4L,
0L), d.y = c(2L, 0L)), row.names = c(NA, -2L), class = …Run Code Online (Sandbox Code Playgroud) 我有这个小题:
\nlibrary(tibble)\nlibrary(dplyr)\n\ndf <- tibble(id = c("one", "two", "three"),\n A = c(1,2,3), \n B = c(4,5,6))\n\n id A B\n <chr> <dbl> <dbl>\n1 one 1 4\n2 two 2 5\n3 three 3 6\nRun Code Online (Sandbox Code Playgroud)\n我想向每个组添加一行,并为新列分配值,但使用一个函数(这里每个组中的新行应该= USINGA=4 B列的第一个组值->所需的输出:B first(B)
id A B\n<chr> <dbl> <dbl>\n1 one 1 4\n2 one 4 4\n3 three 3 6\n4 three 4 6\n5 two 2 5\n6 two 4 5\nRun Code Online (Sandbox Code Playgroud)\n到目前为止我已经尝试过:
\n如果我在未分组的小标题中添加一行add_row-> 这很完美!
df …Run Code Online (Sandbox Code Playgroud) 这是我的拖放应用程序:
\nlibrary(shiny)\nlibrary(shinyjqui)\nlibrary(shinyjs)\nlibrary(dplyr)\n\n\n###### part 1 ------------------------------------------------------------------\n#creating the list of items\ndf <- structure(list(AG = c("A", "B", "C", "D")),\n row.names = c(NA,-4L), class = "data.frame")\n\n# cells of table\nconnections1 <- paste0("droppable_cell", ifelse(1:2 == 1, "", 1:2), "_1")\nconnections2 <- paste0("droppable_cell", ifelse(1:2 == 1, "", 1:2), "_2")\n\nconnections <- c(connections1, connections2)\n\n# Define a named list for vec_suggestion1 \nvec_suggestion1 <- list( \n droppable_cell_1 = c("A", "B", "A", "B"),\n droppable_cell_2 = c("A", "B", "B", "A")\n)\n\n# Create the data frame\nmy_df <- data.frame(connections = connections,\n stringsAsFactors = FALSE\n)\n\nmy_df$vec_suggestion1 <- vec_suggestion1[my_df$connections]\n\n\n###### …Run Code Online (Sandbox Code Playgroud) 是否可以cumsum()在另一列上以开始 - 停止条件迭代地使用一列:
df具有一列的数据框,X其中值是递增的。cumsum() 应在达到 10 或 10 的倍数时停止(例如 20、30、40,...)。cumsum则应在最后一次出现 10、20、30、40 时停止...这是数据帧:df <- structure(list(X = c(55L, 95L, 39L, 52L, 22L, 93L, 76L, 82L,
77L, 58L, 60L, 19L, 31L, 43L, 65L, 56L, 18L, 66L, 21L, 49L, 13L,
37L, 36L, 51L, 41L, 7L, 91L, 3L, 11L, 65L, 51L, 32L, 25L, 10L,
5L, 7L, 8L, 3L, 72L, 66L, 93L, 24L, 48L, 44L, 91L, 60L, 62L,
89L, …Run Code Online (Sandbox Code Playgroud)