[英]Combine two lists when creating dataframe R
I have the following situtation with two list
: Path
and UTMZones
我有以下两个list
UTMZones
: Path
和UTMZones
> 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/"
The second list (the Null is okay) 第二个列表(Null可以)
> UTMZones
[[1]]
NULL
[[2]]
[1] "30"
[[3]]
[1] "18"
Using this as input I am creating a df
with the following code: 使用此作为输入,我将使用以下代码创建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"))
The code works but now I need to insert a small modification. 该代码有效,但是现在我需要插入一个小的修改。 In the last part of the code, when creating the dataframe I am doing 在代码的最后部分,当我创建数据框时
~ paste0("class org.esa.s2tbx.dataio.s2.ortho.plugins.Sentinel2L1CProduct_Multi_UTM", UTMZones, "N_ReaderPlugIn", collapse = "")))
This code of course does not work. 此代码当然不起作用。 What I am trying to do is that when creating the df
, for position [i]
in Path
, the first position of UTMZones
(eg [j]
should be used in the paste
function 我想做的是,在创建df
,对于Path
位置[i]
, UTMZones
的第一个位置(例如[j]
应该在paste
功能中使用)
I have been trying with a two-variable for
loop but I do not get the right result: 我一直在尝试使用两个变量的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 = "")))
}
}
-- EDIT -- -编辑-
The output should look like this. 输出应如下所示。 Note how the UTM changes in sequence using UTMZones
as reference. 请注意如何使用UTMZones
作为参考来更改UTM的顺序。
> 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...
-- EDIT 2 -- -编辑2-
This is the run of the code with @Ronak Shah solution 这是@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"
Few changes in the code. 代码中很少有更改。 First replace the NULL
elements to blank elements 首先将NULL
元素替换为空白元素
UTMZones[lengths(UTMZones) == 0] <- ""
then include UTMZones
in the dataframe so that it is easy to replace values. 然后在数据UTMZones
包含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")))
We can do this in base R
我们可以在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"))})
If we don't need the 'UTM' column 如果我们不需要“ 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")))
Although @akrun and @Ronak Shah answers are much more efficient and definitely this is what I am looking for, I'll put down my - not perfect - attempt which is much more basic but I would say easy to follow in case someone is interested. 尽管@akrun和@Ronak Shah的答案要有效得多,而且绝对是我想要的,但我会放下我的(不是完美的)尝试,这种尝试更为基本,但是如果有人感兴趣,我会说易于理解。
Since I was not managing to itereate in the dataframe properly, once having the dataframe created with a wrong content for the plugin
column, I used the following code to correct it. 由于我没有设法正确地在数据框中进行迭代,因此一旦为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 = "")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.