从数据框中提取数值,一行代码。

huangapple go评论49阅读模式
英文:

Extract values from a data frame in one line

问题

我有一个包含多边形的xy坐标的数据框,例如

head(df)
         x        y
1 -80.59264 40.88406
2 -80.50539 40.91076
3 -80.41909 40.93284
4 -80.32967 40.94047
5 -80.24228 40.93894
6 -80.16811 40.93434

我尝试提取它以获得像这样的结果:

df
(-80.59264 40.88406, -80.50539 40.91076, -80.41909 40.93284, -80.32967 40.94047, -80.24228 40.93894, -80.16811 40.93434...)

即每一行由逗号分隔,列值用空格分隔。

这在R中是可以做到的吗?

感谢您的建议。

英文:

I have a data frame with xy coordinates of a polygon, such as

head(df)
         x        y
1 -80.59264 40.88406
2 -80.50539 40.91076
3 -80.41909 40.93284
4 -80.32967 40.94047
5 -80.24228 40.93894
6 -80.16811 40.93434

I try to extract it to obtain something like this:

df
(-80.59264 40.88406, -80.50539 40.91076, -80.41909 40.93284, -80.32967 40.94047, -80.24228 40.93894, -80.16811 40.93434...)

i.e. each line is separated by a comma and column values with a space.

This is possible to do this in R?

Thank you for your suggestions.

答案1

得分: 4

使用 paste

do.call(paste, c(df, collapse = ", "))
英文:

Perhaps use paste

do.call(paste, c(df, collapse = ", "))

huangapple
  • 本文由 发表于 2023年3月31日 22:35:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定