我有一个数据框,其中包含一列字符串,如下所示:
mydata <- c("-1.356670,35.355030",
"-1.356670,35.355030",
"-1.356620,35.355890",
"-1.356930,35.358660",
"-1.357000,35.359060"
)
df <- data.frame(mydata)
我想将其转换为包含两列“ long
和lat
的数据帧,每列都是数字类型。这样做的最佳方法是什么?我尝试使用{{ 1}},但似乎无法使其正常工作。
答案 0 :(得分:3)
使用基数R,您可以执行以下操作:
df$Long <- as.numeric(sapply(strsplit(as.character(df$mydata), ","), function(x) x[1]))
df$Lat <- as.numeric(sapply(strsplit(as.character(df$mydata), ","), function(x) x[2]))
mydata Long Lat
1 -1.356670,35.355030 -1.35667 35.35503
2 -1.356670,35.355030 -1.35667 35.35503
3 -1.356620,35.355890 -1.35662 35.35589
4 -1.356930,35.358660 -1.35693 35.35866
5 -1.357000,35.359060 -1.35700 35.35906
或者使用tstrsplit()
中的data.table
:
df$Long <- as.numeric(tstrsplit(df$mydata, ",")[[1]])
df$Lat <- as.numeric(tstrsplit(df$mydata, ",")[[2]])
还有@clmarquart提出的tstrsplit()
中的data.table
:
setDT(df)[, c("lat", "long") := tstrsplit(mydata, ",", fixed = TRUE)]
答案 1 :(得分:3)
这可以在基数R的一行中完成:
public class CityInfoComparer:IComparer<CityInfo>
{
private readonly IComparer<string> _baseComparer;
public CityInfoComparer(IComparer<string> baseComparer)
{
_baseComparer = baseComparer;
}
public int Compare(CityInfo city1, CityInfo city2)
{
return _baseComparer.Compare(city1.CityName, city2.CityName);
}
}
public class CityList
{
public List<CityInfo> CityInfos { get; set; }
public void Sort()
{
CityInfos.Sort( new CityInfoComparer(StringComparer.CurrentCulture));
}
}
给予:
read.table(text = as.character(df$mydata), sep = ",", col.names = c("long", "lat"))
答案 2 :(得分:1)
一种tidyverse
解决方案。
library(tidyverse)
dat <- df %>%
separate(mydata, into = c("Long", "Lat"), sep = ",", convert = TRUE)
# Print the data
dat
# Long Lat
# 1 -1.35667 35.35503
# 2 -1.35667 35.35503
# 3 -1.35662 35.35589
# 4 -1.35693 35.35866
# 5 -1.35700 35.35906
答案 3 :(得分:0)
将strsplit
与do.call
一起使用,那么我们只需要分配列名
newdf=do.call(rbind.data.frame, strsplit(mydata,','))
names(newdf)=c('long','lat')
newdf
long lat
1 -1.356670 35.355030
2 -1.356670 35.355030
3 -1.356620 35.355890
4 -1.356930 35.358660
5 -1.357000 35.359060