我有以下令人讨厌的嵌套列表
编辑:更新以包括value_I_dont_want
mylist <- list(
list(
nested_1 = list(
nested_2 = list(
list( value_I_want = "a", value_I_dont_want = "f"),
list( value_I_want = "b", value_I_dont_want = "g")
)
)
),
list(
nested_1 = list(
nested_2 = list(
list( value_I_want = "c", value_I_dont_want = "h"),
list( value_I_want = "d", value_I_dont_want = "i"),
list( value_I_want = "e", value_I_dont_want = "j")
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
我想得到所有value_I_want的
我知道我可以在 for 循环中使用以下代码
mylist[[x]]$nested_1$nested_2[[y]]$value_I_want
Run Code Online (Sandbox Code Playgroud)
但我想提高我的地图技能。map_chr我了解当列表是单个级别时如何使用,但我没有找到很多关于从非常嵌套的列表中提取的资源。我也知道我可以使用,[[但还没有找到合适的文档?
任何帮助表示赞赏!
我想合并两个列表
list_1 <- list(LIST1 = list(list("a"), list("b"), list("c")))
list_2 <- list(LIST2 = list(list("1"), list("2"), list("3")))
Run Code Online (Sandbox Code Playgroud)
combined_list <- list()
combined_list[[1]] <- c("a", "1")
combined_list[[2]] <- c("b", "2")
combined_list[[3]] <- c("c", "3")
Run Code Online (Sandbox Code Playgroud)
我有一个讨厌的for循环方式,但是我想使用purrr清理它吗?任何帮助表示赞赏!
我正在尝试创建一个闪亮的应用程序,用户可以选择要绘制的 x 和 y 轴,然后单击“添加图形”以绘制图形 - 但此代码不起作用,我不确定为什么。我需要将input$x_iris和设置input$y_iris为反应式吗?
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("4 Plot Generator"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(6, selectInput("x_iris", "X:", choices= c(unique(colnames(iris))))),
column(6, selectInput("y_iris", "Y:", choices = c(unique(colnames(iris)))))
),
# button to add graph
# button to remove graph
fluidRow(
column(6, actionButton("add_graph", "Add Graph")),
column(6, actionButton("reset_graph", "Reset Graphs"))
)
),
# Show graph
mainPanel(
plotOutput("plots")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
observeEvent(input$add_graph,{
# not …Run Code Online (Sandbox Code Playgroud) 我试图为plotly'ssubplot函数中的每个图表提供单独的标题,我发现了一篇文章,您可以使用它来扩展子图,%>% layout(title = "Main Title)但我想为每个图表提供单独的标题(我正在使用,ggtitle但只绘制最后一个标题提供(图 4)。我发现了类似的帖子为每个子图提供标题 - R Shiny但我不认为我可以facet_wrap在我的场景中。
此外 - 我想知道如何增加子图中图表之间的边距,因为它们似乎真的被压在一起了。
任何帮助表示赞赏!
ui <- fluidPage(
sidebarPanel("This is a sidebar"),
mainPanel(plotlyOutput("myplot"))
)
server <- function(input, output){
output$myplot <- renderPlotly({
gg1 <- ggplotly(
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
theme_minimal() +
ggtitle("Plot 1")
)
gg2 <- ggplotly(
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
theme_minimal() +
ggtitle("Plot 2")
)
gg3 <- ggplotly(
ggplot(iris, aes(x=Petal.Width)) +
geom_histogram() +
ggtitle("Plot 3")
) …Run Code Online (Sandbox Code Playgroud) 我想右对齐,rowname_col但看起来您不能应用于cols_align行名?
tibble(
one = c("Long names", "Other Name", "Name | Name"),
two = 1:3
) %>% gt(rowname_col = "one") %>%
cols_align(align = "right", columns = vars(one))
Run Code Online (Sandbox Code Playgroud)
我有一个数据框列表,我想x从每个数据框中获取列作为字符串。
testing <- list(data.frame(A = "Yes", B = "No"),
data.frame(B = "No", C = "No"),
data.frame(A = "Yes"))
Run Code Online (Sandbox Code Playgroud)
我可以打印哪些数据帧中有colnameA,但我无法连接到子集原始测试
lapply(testing, function(x) "A" %in% colnames(x))
Run Code Online (Sandbox Code Playgroud)
[[1]]
A B
1 Yes No
[[2]]
A
1 Yes
Run Code Online (Sandbox Code Playgroud) 我有一个数据框列表
vars <- c("Col_1", "Col_2", "Col_3")
list_df <- list(
tibble(
a = 1,
b = 2,
c = 3
),
tibble (
d = 1,
e = 2,
f = 3
)
)
Run Code Online (Sandbox Code Playgroud)
我想使用 重命名列,vars但我想我在这里遗漏了一些东西。
map(list_df, ~rename_all(.x, vars))
Run Code Online (Sandbox Code Playgroud)
结果应该是:
list(
tibble(
Col_1 = 1,
Col_2 = 2,
Col_3 = 3
),
tibble (
Col_1 = 1,
Col_2 = 2,
Col_3 = 3
)
)
Run Code Online (Sandbox Code Playgroud)
无论初始列名如何,我都希望它可以工作(并且所有数据框始终只有三列。这可能吗?
我想知道我们是否可以使用purrr'smap和reduce来创建类似于 JavaScript 中的这个函数的斐波那契函数:
function fib(n){
return new Array(n).fill(1).reduce((arr, _ ,i) => {
arr.push((i <= 1) ? i : arr[i-2] + arr[i-1])
return arr
},[]) ;
}
console.log(fib(10))
Run Code Online (Sandbox Code Playgroud)
我在这里看到R中使用递归的斐波那契数列的答案,但我想知道我们是否可以专门使用 purrr reduce,如果可以,如何使用?
我看过很多帖子,所以如果这是多余的,我很抱歉,但希望获得一些帮助来压平嵌套列表:
test <- list()
test <- c(
list("A" = c(list("1"), list("2"), list("3"))),
list("B" = c(list("4"), list("5"), list("6")))
)
Run Code Online (Sandbox Code Playgroud)
name subcat
1 A 1
2 A 2
3 A 3
4 B 4
5 B 5
6 B 6
Run Code Online (Sandbox Code Playgroud)
我正在努力编写一个嵌套的 for 循环,但我真的很想使用 purrr 或更优雅的东西来创建一个包含两列的数据框:子目录列和列表中每个元素的名称的重复列。
感谢您的任何帮助,甚至只是向我指出类似的帖子 - 谢谢!