R元组作为因子(特别是经度线性因子)

时间:2016-08-11 03:51:18

标签: r indexing tuples

我在访问R中的因素方面遇到了问题。我有一个元组因子

的数据框
test1
#[1] (34.0467, -118.2470) (34.0637, -118.2440) (34.0438, -118.2547)
#[4] (34.0523, -118.2676) (34.0584, -118.2810) (34.0583, -118.2616)
#39497 Levels: (0, 0) (0.0000, 0.0000) ... (34.6837, -118.1853)

如何只访问元组的第一个数字?

谢谢!

  

dput(TEST1)   ...   "(34.3256,-118.4307)","(34.3256,-118.4798)","(34.3256,-118.5033)",   "(34.3257,-118.4244)","(34.3258,-118.4343)","(34.3262,-118.4104)",   "(34.3262,-118.4112)","(34.3266,-118.4234)","(34.3266,-118.4269)",   "(34.3266,-118.4323)","(34.3269,-118.4278)","(34.3272,-118.4365)",   "(34.3273,-118.4342)","(34.3274,-118.4321)","(34.3274,-118.4331)",   "(34.3275,-118.4247)","(34.3275,-118.4298)","(34.3276,-118.4115)",   "(34.3277,-118.4071)","(34.3285,-118.4266)","(34.3286,-118.4277)",   "(34.3287,-118.4286)","(34.3292,-118.5048)","(34.3293,-118.4246)",   "(34.3298,-118.4300)","(34.3327,-118.5062)","(34.3374,-118.5042)",   "(34.3760,-118.5254)","(34.3767,-118.5263)","(34.3775,-118.5270)",   "(34.3805,-118.5293)","(34.4638,-118.1995)","(34.5095,-117.9273)",   "(34.5304,-118.1418)","(34.5453,-118.0405)","(34.5650,-118.0856)",   "(34.5693,-118.0228)","(34.5957,-118.1784)","(34.6818,-118.0954)",   "(34.6837,-118.1853)"),class =" factor")

无论如何都无法开始。

3 个答案:

答案 0 :(得分:1)

test1 <- factor(c("(34.3242, -118.4494)", "(34.3242, -118.4914)", "(34.3243, -118.4167)"))

首先,将因子向量转换为字符向量。

test1 <- as.character(test1)

然后,删除所有(),并将字符串拆分为,

test1 <- gsub("\\(|\\)", "", test1)
test1 <- strsplit(test1, ",")

之后,将数字从字符格式更改为数字格式。

test1 <- lapply(test1, as.numeric)

最后,获取每个点的第一个坐标(如果您想要第二个坐标,请将1更改为2。)

test1 <- unlist(lapply(test1, '[[', 1))

这是输出。

> test1
[1] 34.3242 34.3242 34.3243

答案 1 :(得分:0)

再次索引

x[1][1]
x[2][1]

答案 2 :(得分:0)

试试这个

as.numeric(unlist(strsplit(gsub("[\\(\\)]", "",as.character(test1)),","))[c(T,F)])

<强>解释

gsub仅适用于character。因此,as.character(test1)正在将test1character转换为factor。然后我就像这样删除"(" & ")"

gsub("[\\(\\)]", "",as.character(test1))
#[1] "34.5693, -118.0228" "34.5957, -118.1784" "34.6818, -118.0954" "34.6837, -118.1853"

稍后我将它们分成两部分,具体取决于分隔符,

strsplit(gsub("[\\(\\)]", "",as.character(test1)),",")
#[[1]]
#[1] "34.5693"    " -118.0228"

#[[2]]
#[1] "34.5957"    " -118.1784"

#[[3]]
#[1] "34.6818"    " -118.0954"

#[[4]]
#[1] "34.6837"    " -118.1853"

上一个输出是一个列表。 unlist输出了一个向量。

unlist(strsplit(gsub("[\\(\\)]", "",as.character(test1)),","))
#[1] "34.5693"    " -118.0228" "34.5957"    " -118.1784" "34.6818"    " -118.0954"
#[7] "34.6837"    " -118.1853"

基本上[c(T,F)]正在生成TRUEFALSE的交替序列,用于选择第一个元素。

最后,我使用numeric

创建了输出as.numeric

<强>输出

#[1] 34.5693 34.5957 34.6818 34.6837