[英]R create column in dataframe value name of dataframe
I have a list of dataframes (these are spatial dataframes) named for example "map_g1_r1_airport", "map_g1_r1_hotel", "map_g1_r2_bank", "map_g1_r2_market"我有一个数据框列表(这些是空间数据框),例如“map_g1_r1_airport”、“map_g1_r1_hotel”、“map_g1_r2_bank”、“map_g1_r2_market”
These are elements that were digitized from several maps.这些是从几张地图中数字化的元素。 The maps were originally called "map_g1_r1", "map_g1_r2".
这些地图最初被称为“map_g1_r1”、“map_g1_r2”。
I am trying to add a column to each dataframe with the name of the original map using a loop.我正在尝试使用循环向每个 dataframe 添加一列,其名称为原始 map。
Here is what I am trying to do:这是我正在尝试做的事情:
map_g1_r1_airport$mapid<-map_g1_r1
With the loop (Unfortunately this does not do what I intend to do. Instead it simply creates a "content" field in the Values board.):
list_df<-c("map_g1_r1_airport", "map_g1_r1_hotel", "map_g1_r2_bank", "map_g1_r2_market")
for (df in 1:length(list_df)){
paste(list_df[df],"$mapid<-",
print(content<-gsub("(.*)_.*","\\1",
c(paste(list_df[df]))),sep=""),
quote=FALSE)}
Any help is most welcome!任何帮助都是最受欢迎的!
Here is one example of the data before change:以下是更改前数据的一个示例:
structure(list(id = c(1, 2, 3), Name = structure(c(1L, 3L, 4L
), .Label = c("A", "B", "C", "D", "E"
), class = "factor"), Year = structure(c(NA_integer_, NA_integer_,
NA_integer_), .Label = character(0), class = "factor"), geometry = structure(list(
structure(c(41.4086152370865, 2.44718243982123), class = c("XY",
"POINT", "sfg")), structure(c(45.3852740543083, -4.31103098867136
), class = c("XY", "POINT", "sfg")), structure(c(38.4200314592624,
-6.96113884231683), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
"sfc"), precision = 0, bbox = structure(c(xmin = 41.4086152370865,
ymin = 2.31103098867136, xmax = 45.4200314592624, ymax = -4.44718243982123
), class = "bbox"), crs = structure(list(epsg = NA_integer_,
proj4string = NA_character_), class = "crs"), n_empty = 0L)), sf_column = "geometry", agr = structure(c(id = NA_integer_,
Name = NA_integer_, Year = NA_integer_), .Label = c("constant",
"aggregate", "identity"), class = "factor"), row.names = c(NA,
3L), class = c("sf", "data.frame"))
This is what I would like to get (with the mapid map_g1_r1 ):这就是我想要得到的(使用 mapid map_g1_r1 ):
structure(list(id = c(1, 2, 3), Name = structure(c(1L, 3L, 4L
), .Label = c("A", "B", "C", "D", "E"
), class = "factor"), Year = structure(c(NA_integer_, NA_integer_,
NA_integer_), .Label = character(0), class = "factor"), geometry = structure(list(
structure(c(41.4086152370865, 2.44718243982123), class = c("XY",
"POINT", "sfg")), structure(c(45.3852740543083, -4.31103098867136
), class = c("XY", "POINT", "sfg")), structure(c(38.4200314592624,
-6.96113884231683), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
"sfc"), precision = 0, bbox = structure(c(xmin = 41.4086152370865,
ymin = 2.31103098867136, xmax = 45.4200314592624, ymax = -4.44718243982123
), class = "bbox"), crs = structure(list(epsg = NA_integer_,
proj4string = NA_character_), class = "crs"), n_empty = 0L),
mapid = c("map_g1_r1", "map_g1_r1", "map_g1_r1")), sf_column = "geometry", agr = structure(c(id = NA_integer_,
Name = NA_integer_, Year = NA_integer_, mapid = NA_integer_), .Label = c("constant",
"aggregate", "identity"), class = "factor"), row.names = c(NA,
3L), class = c("sf", "data.frame"))
You can achieve that even without a loop.即使没有循环,您也可以实现这一目标。
I would first start by creating a list with the names you want to see in each spatial data.frame.我将首先创建一个列表,其中包含您希望在每个空间 data.frame 中看到的名称。 I assume they are derived from the names of the list.
我假设它们来自列表的名称。
mapid = names(list_df)
following that you can employ mapply
to use a function that takes the first element of a list (or vector) and the first element of another list/vector.之后,您可以使用
mapply
来使用 function ,它采用列表(或向量)的第一个元素和另一个列表/向量的第一个元素。 Them it moves on and apply the same function to the second elements of each vector.它们继续前进并将相同的 function 应用于每个向量的第二个元素。 It is essentially a multiple input version of
lapply
.它本质上是
lapply
的多输入版本。
The function we will give to mapply
is cbind
which creates takes takes two data.frames and joins them by column.我们将给 mapply 的
mapply
是cbind
,它创建需要两个 data.frames 并按列连接它们。 In this case one data.frame will be your spatial object and the other will be a vector with one single element: the current map name.在这种情况下,一个 data.frame 将是您的空间 object ,另一个将是具有一个元素的向量:当前 map 名称。
cbind
will naturally convert this name to a 1-column data.frame and repeat the name to match the number of rows in the spatial object. cbind
会自然地将此名称转换为 1 列 data.frame 并重复该名称以匹配空间 object 中的行数。
final = mapply(cbind, list_df, mapid)
I haven't tested it, but it should work.我还没有测试过,但它应该可以工作。
You can get all the individual dataframes in a list using mget
and add a new column with their name using mutate
.您可以使用
mget
获取列表中的所有单个数据框,并使用mutate
添加具有其名称的新列。
Using tidyverse
functions you can do this as:使用
tidyverse
函数,您可以这样做:
library(dplyr)
library(purrr)
list_df<-c("map_g1_r1_airport", "map_g1_r1_hotel", "map_g1_r2_bank", "map_g1_r2_market")
tmp <- mget(list_df)
result <- imap(tmp, ~.x %>% mutate(map_id = .y))
result
will have all changed dataframes in a list, if you want these changes to reflect in the original object you can use list2env
. result
将在列表中包含所有更改的数据帧,如果您希望这些更改反映在原始 object 中,您可以使用list2env
。
list2env(result, .GlobalEnv)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.