且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何重新格式化数据并进行映射?

更新时间:2023-10-04 22:47:52

只需转置数据:

library(dplyr)  #for mutate, if you so choose to use it
df.new <- t(df)
df.new <- as.data.frame(df.new) #if you want a dataframe instead
df.new <- df.new %>% mutate(City=rownames(.))

输出:

         lon      lat            City
1  -82.99879 39.96118        Columbus
2   -86.7816 36.16266       Nashville
3  -97.74306 30.26715          Austin
4  -77.03687 38.90719 Washington D.C.
5 -0.1277583 51.50735          London
6  -2.242631 53.48076      Manchester

我喜欢管道,因此您可以只写一行:

I love pipes, so you could just write one line:

df %>% t() %>% as.data.frame() %>% mutate(City=rownames(.))