将列表元素连接到 R 中新列中的特定格式
Concatenate elements of list to specific format in new column in R
我有关联的 "subID"s 的 latlon 坐标。我想在此数据框中创建一个新列,其中每个子 ID 都已粘贴为 html 格式。见下文:
我的数据:
latlon subID
1 25.4034735, -80.5586135 c("S35858790", "S35858833", "S35924843")
2 26.4330582, -80.9416786 c("S35834082", "S35857972")
3 26.452893, -80.979942 S35686789
4 29.3339241, -94.7480679 c("S20299537", "S20300308")
我想在新列的第一行看到的内容(向右滚动):
<a href = http://www.SomeUrl.com/S35858790>S35858790</a><a href = http://www.SomeUrl.com/S35858833>S35858833</a><a href = http://www.SomeUrl.com/S35924843>S35924843</a>
第三行应该只有:
<a href = http://www.SomeUrl.com/S35686789>S35686789</a>
subID 列可以是最多或超过 100 个 subID 的列表。
这与 没有太大区别。同样,我们可以使用基础 R 函数 aggregate
,这次使用 paste
来构建您要使用的锚标记:
a_start <- '<a href = http://www.SomeUrl.com/'
a_end <- '</a>'
out <- aggregate(data=df,subID~latlon,FUN = function(t) sort(paste0(a_start, t, '>', t, a_end, collapse="")))
即使我们的自定义聚合函数对整个锚标记进行排序,它仍然应该正确排序,因为第一个 subID
之前的内容对于所有内容都是相同的。
您也可以使用 tapply
,尽管这会给您一个向量。
funfun=function(x)paste0("<a href = http://www.SomeUrl.com/",x,">",x,"</a>",collapse = "")
with(data,tapply(subID,latlon,funfun))
我有关联的 "subID"s 的 latlon 坐标。我想在此数据框中创建一个新列,其中每个子 ID 都已粘贴为 html 格式。见下文:
我的数据:
latlon subID
1 25.4034735, -80.5586135 c("S35858790", "S35858833", "S35924843")
2 26.4330582, -80.9416786 c("S35834082", "S35857972")
3 26.452893, -80.979942 S35686789
4 29.3339241, -94.7480679 c("S20299537", "S20300308")
我想在新列的第一行看到的内容(向右滚动):
<a href = http://www.SomeUrl.com/S35858790>S35858790</a><a href = http://www.SomeUrl.com/S35858833>S35858833</a><a href = http://www.SomeUrl.com/S35924843>S35924843</a>
第三行应该只有:
<a href = http://www.SomeUrl.com/S35686789>S35686789</a>
subID 列可以是最多或超过 100 个 subID 的列表。
这与 aggregate
,这次使用 paste
来构建您要使用的锚标记:
a_start <- '<a href = http://www.SomeUrl.com/'
a_end <- '</a>'
out <- aggregate(data=df,subID~latlon,FUN = function(t) sort(paste0(a_start, t, '>', t, a_end, collapse="")))
即使我们的自定义聚合函数对整个锚标记进行排序,它仍然应该正确排序,因为第一个 subID
之前的内容对于所有内容都是相同的。
您也可以使用 tapply
,尽管这会给您一个向量。
funfun=function(x)paste0("<a href = http://www.SomeUrl.com/",x,">",x,"</a>",collapse = "")
with(data,tapply(subID,latlon,funfun))