首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建dataframe R时合并两个列表

创建dataframe R时合并两个列表
EN

Stack Overflow用户
提问于 2019-08-28 11:29:57
回答 3查看 101关注 0票数 2

对于两个list,我有以下情况:PathUTMZones

代码语言:javascript
复制
> 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/"

第二个列表(空号没问题)

代码语言:javascript
复制
> UTMZones
[[1]]
NULL

[[2]]
[1] "30"

[[3]]
[1] "18"

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

代码语言:javascript
复制
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"))

代码可以工作,但现在我需要插入一个小的修改。在代码的最后一部分中,当创建我正在做的数据访问时

代码语言:javascript
复制
 ~ paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", UTMZones, "N_ReaderPlugIn", collapse = "")))

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

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

代码语言:javascript
复制
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作为参考的顺序变化的。

代码语言:javascript
复制
> 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解决方案运行的代码

代码语言:javascript
复制
>     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"
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-08-28 14:09:04

我们可以在base R中做到这一点

代码语言:javascript
复制
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”列

代码语言:javascript
复制
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")))
票数 1
EN

Stack Overflow用户

发布于 2019-08-28 14:00:52

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

代码语言:javascript
复制
UTMZones[lengths(UTMZones) == 0] <- ""

然后在dataframe中包括UTMZones,这样就可以很容易地替换值。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-08-28 14:37:36

虽然@akrun和@Ronak的回答效率要高得多,这绝对是我想要的,但我会把我的--不是完美的--尝试写下来,这是更基本的尝试,但如果有人感兴趣的话,我会说很容易跟踪。

由于我无法正确地迭代dataframe,一旦为plugin列创建了带有错误内容的dataframe,我使用下面的代码来更正它。

代码语言:javascript
复制
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 = "")
      }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57691388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档