创建数据框R时合并两个列表

时间:2019-08-28 11:29:57

标签: r list dataframe for-loop

我在下面的情况中有两个listPathUTMZones

> Path
[[1]]
[1] "/home/rus/S1A_IW_GRDH_1SDV_20190824T003615_20190824T003640_028704_033FD2_7CC8.SAFE/"

[[2]]
[1] "/home/rus/S2A_MSIL2A_20190827T105621_N0213_R094_T30TVK_20190827T141656.SAFE/"

[[3]]
[1] "/home/rus/S2B_MSIL2A_20190826T153819_N0213_R011_T18TXL_20190826T195901.SAFE/"

第二个列表(Null可以)

> UTMZones
[[1]]
NULL

[[2]]
[1] "30"

[[3]]
[1] "18"

使用此作为输入,我将使用以下代码创建一个df

df<-enframe(Path, name = "number", value = "uri") %>%
        unnest %>%
        mutate(plugin = case_when(substr(uri, 11, 12) == "S1" ~ "class org.esa.s1tbx.io.sentinel1.Sentinel1ProductReaderPlugIn", TRUE ~ "class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM18N_ReaderPlugIn"))

该代码有效,但是现在我需要插入一个小的修改。在代码的最后一部分中,当我创建数据框时

 ~ paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", UTMZones, "N_ReaderPlugIn", collapse = "")))

此代码当然不起作用。我想做的是,在创建df时,对于[i]中的位置Path,应使用UTMZones的第一个位置(例如[j]paste函数

我一直在尝试使用两个变量的for循环,但是没有得到正确的结果:

for (i in seq_along(Path)){
      for(j in seq_along(UTMZones)){

        df<-enframe(Path[[i]], name = "number", value = "uri") %>%
        unnest %>%
        mutate(plugin = case_when(substr(uri, 11, 12) == "S1" ~ "class org.esa.s1tbx.io.sentinel1.Sentinel1ProductReaderPlugIn", TRUE ~ paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", UTMZones[[j]], "N_ReaderPlugIn", collapse = "")))

      }
    }

-编辑-

输出应如下所示。请注意 UTM 如何以UTMZones为参考顺序变化。

> df
# A tibble: 5 x 3
  number uri                                                                     plugin                                                                      
   <int> <chr>                                                                   <chr>                                                                       
1      1 /home/rus/S1A_IW_GRDH_1SDV_20190824T003615_20190824T003640_028704_033F? class org.esa.s1tbx.io.sentinel1.Sentinel1ProductReaderPlugIn               
2      2 /home/rus/S2A_MSIL2A_20190827T105621_N0213_R094_T30TVK_20190827T141656? class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM30...
3      3 /home/rus/S2B_MSIL2A_20190826T153819_N0213_R011_T18TXL_20190826T195901? class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM18...

-编辑2-

这是使用@Ronak Shah解决方案运行的代码

>     UTMZones[lengths(UTMZones) == 0] <- ""
> library(tidyverse)
> df<-enframe(Path, name = "number", value = "uri") %>%
+       mutate(UTM  = UTMZones) %>%
+       unnest %>%  
+       mutate(plugin = ifelse(substr(uri, 11, 12) == "S1", 
+                              "class org.esa.s1tbx.io.sentinel1.Sentinel1ProductReaderPlugIn", 
+                              paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", 
+                                     UTM, "N_ReaderPlugIn", collapse = "")))
> df$plugin[[3]]
[1] "class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTMN_ReaderPlugInclass org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM30N_ReaderPlugInclass org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM18N_ReaderPlugIn"

3 个答案:

答案 0 :(得分:1)

代码中很少有更改。首先将NULL元素替换为空白元素

UTMZones[lengths(UTMZones) == 0] <- ""

然后在数据框中添加UTMZones,以便于替换值。

library(tidyverse)

enframe(Path, name = "number", value = "uri") %>%
    mutate(UTM  = UTMZones) %>%
    unnest %>%  
    mutate(plugin = ifelse(substr(uri, 11, 12) == "S1", 
    "class org.esa.s1tbx.io.sentinel1.Sentinel1ProductReaderPlugIn", 
paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", 
            UTM, "N_ReaderPlugIn")))

答案 1 :(得分:1)

我们可以在base R

中完成此操作
UTMZones <- lapply(UTMZones, function(x) replace(x, is.null(x), ""))
within(stack(setNames(Path, seq_along(Path)))[2:1],{ UTM <- unlist(UTMZones);plugin <- ifelse(substr(values, 11, 12) == "S1", 
 "class org.esa.s1tbx.io.sentinel1.Sentinel1ProductReaderPlugIn", 
    paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", 
         UTM, "N_ReaderPlugIn"))})

如果我们不需要“ UTM”列

transform(stack(setNames(Path, seq_along(Path)))[2:1], 
    plugin= ifelse(substr(values, 11, 12) == "S1", 
 "class org.esa.s1tbx.io.sentinel1.Sentinel1ProductReaderPlugIn", 
    paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", 
         unlist(UTMZones), "N_ReaderPlugIn")))

答案 2 :(得分:1)

尽管@akrun和@Ronak Shah的答案要有效得多,而且绝对是我要的答案,但我会放下我的-并非完美的-尝试,这是更基本的尝试,但我会说易于遵循有人感兴趣。

由于我无法正确地在数据框中进行迭代,因此一旦为plugin列创建了错误的内容后,我便使用以下代码对其进行了纠正。

for (i in seq_along(Path)){
      if (substr(Path[[i]], 11,12) == 'S1') {
        df$plugin[[i]] <- 'class org.esa.s1tbx.io.sentinel1.Sentinel1ProductReaderPlugIn'
      } else {
        df$plugin[[i]] <- paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", UTMZones[[i]], "N_ReaderPlugIn", collapse = "")
      }
    }