这个问题建立在我之前的问题之上。
我有一个列表c("String1","String2","String3")
如果没有循环,代码看起来像这样
Data_String1<- DF2 %>%
mutate(`New String`= if_else(lengths(st_intersects(DF2, DataFrame_String1, join = st_within))>0,DataFrame_String1$ID,"N"))%>%
filter(.,`New String`!="N")
Data_String2<- DF2 %>%
mutate(`New String`= if_else(lengths(st_intersects(DF2, DataFrame_String2, join = st_within))>0,DataFrame_String2$ID,"N"))%>%
filter(.,`New String`!="N")
Data_String3<- DF2 %>%
mutate(`New String`= if_else(lengths(st_intersects(DF2, DataFrame_String3, join = st_within))>0,DataFrame_String3$ID,"N"))%>%
filter(.,`New String`!="N")
使用前面代码中展示的解决方案的原则,我试图在这里实现:
lst1 <- map(c("String1", "String2", "String3") ~DF2 %>%
mutate(`New String`= if_else(lengths(st_intersects(DF2, c("String1","String2","String3"), join = st_within)) > 0, .x, "N")) %>%
filter(.,`New String`!="N")
然而,我收到一个错误信息,像这样
x没有适用于'st_geometry'的方法应用于类"character"的对象,这让我怀疑数据框是否失去了作为一个空间实体。
只需从先前的解决方案扩展:
df_lst <- map(c("String1", "String2", "String3"), ~ DataFrame %>%
filter(ID == .x)
)
DF2_subs <- map(df_lst, ~ DF2 %>%
mutate(`New String`= if_else(
lengths(st_intersects(DF2, .x, join = st_within)) > 0,
.x$ID,
"N")) %>%
filter(`New String`!="N")
)
否则传递实际数据帧而不是字符串向量
DF2_subs <- map(list(Data_String1, Data_String2, Data_String3), ~ DF2 %>%
mutate(`New String`= if_else(
lengths(st_intersects(DF2, .x, join = st_within)) > 0,
.x$ID,
"N")) %>%
filter(`New String`!="N")
)