简体   繁体   English

R 在 dataframe 中创建列 dataframe 的值名称

[英]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 的mapplycbind ,它创建需要两个 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.

相关问题 如何创建一个 dataframe,其列名以 R 中对象的值为条件? - How to create a dataframe with a column name conditional on an object's value in R? R:使用另一个数据框中的列名,条件和值在一个数据框中创建一个新列 - R: Create a new column in a dataframe, using column name, condition and value from another dataframe 将列名粘贴到 R 中数据框的每个值 - Pasting the column name to each value of a dataframe in R 从列表中的 dataframe 中创建 dataframe 并在 R 中的列中创建最大值 - Create dataframe from dataframe in list with the maximum value in a column in R 根据 R 中的列值,基于现有 dataframe 创建另一个 dataframe - Create another dataframe based on an existing dataframe based on a column value in R 根据列名在 R dataframe 中创建列以制作时间序列 - Create column in R dataframe based on name of the column to make a time series 获取列名称,并使用r将其指定为数据框中未列出列的值 - Get the column name and assign as value in unlisted column in the dataframe using r 将列名称粘贴到 R 中 dataframe 的每个值,但仅在一列中 - Pasting the column name to each value of a dataframe in R but only in one column R-对于数据框中的值,使用该值创建一个新的数据框 - R - for value in dataframe, create a new dataframe with the value R数据框按列名称联接 - R dataframe join by column name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM