簡體   English   中英

如何使用 map2 將列添加到數據框列表

[英]How to use map2 to add column to list of dataframes

我正在嘗試使用 map2 為列表中的每個 dataframe 添加一列

這是我的嘗試:

weather_data <- weather_data %>%
                map2(x, y = c("Place1", "Place2", "Place3", "Place4"), ~ x[["Area"]] = y)

這樣做的想法是為每個 dataframe 添加一個名為“區域”的列。 因此,列表中的第一個 dataframe 將有一個值為“Place1”的列“Area”等等......

非常感謝

這可以像這樣實現:

順便說一句:首先。 arguments 到purrr::map2.x.y

list(mtcars, iris, mtcars, iris) %>%
  purrr::map2(.y = c("Place1", "Place2", "Place3", "Place4"), function(x, y) { x[["Area"]] <- y; x })

我們還可以使用map2中的~符號以及來自dplyrmutate

library(dplyr)
library(purrr)
list(mtcars, iris, mtcars, iris) %>%
    map2(c("Place1", "Place2", "Place3", "Place4"), ~ 
               .x %>%
                   mutate(Area = .y))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM