简体   繁体   English

将逗号分隔的整数长字符串转换为 x 和 y 列

[英]Converting long string of comma delimited integers into x and y columns

My data is a long, single line of values separated by commas where every other value is an x or ay coordinate我的数据是一长行单行值,以逗号分隔,其中每个其他值都是 x 或 y 坐标

Data looks like this: 2622731,1387660,2621628,1444522,2619235,1681640数据如下所示:2622731,1387660,2621628,1444522,2619235,1681640

But I want it to look like this:但我希望它看起来像这样:

2622731,1387660 2622731,1387660

2621628,1444522 2621628,1444522

2619235,1681640 2619235,1681640

Short of going through the entire file and deleting the comma and hitting enter as I did for the example above, how might I automate this in R (or Stata)?除了像上面的示例那样遍历整个文件并删除逗号并按 Enter 键之外,我如何在 R(或 Stata)中自动执行此操作?

In R:在 R 中:

## Read in your data
## data = readLines("path/to/your_file.txt")
## Should get you something like this (using the example in your Q)
data = "2622731,1387660,2621628,1444522,2619235,1681640"
data = unlist(strsplit(data, ","))
data = matrix(as.numeric(data), ncol = 2, byrow = TRUE)
data
#         [,1]    [,2]
# [1,] 2622731 1387660
# [2,] 2621628 1444522
# [3,] 2619235 1681640

At that point, perhaps那个时候,也许

data = as.data.frame(data)
names(data) = c("x", "y")
#         x       y
# 1 2622731 1387660
# 2 2621628 1444522
# 3 2619235 1681640

In Stata the analogue of the accepted R solution might involve split and reshape long .在 Stata 中,公认的 R 解决方案的类似物可能涉及splitreshape long Here is another approach:这是另一种方法:

* data example 
clear
set obs 1
gen strL data = "2622731,1387660,2621628,1444522,2619235,1681640"

* code for data example 
replace data = subinstr(data, ",", " ", .)
set obs `=wordcount(data)/2' 
gen x = real(word(data[1], 2 * _n - 1))
gen y = real(word(data[1], 2 * _n))

list 

     +---------------------------------------------------------------------+
     |                                            data         x         y |
     |---------------------------------------------------------------------|
  1. | 2622731 1387660 2621628 1444522 2619235 1681640   2622731   1387660 |
  2. |                                                   2621628   1444522 |
  3. |                                                   2619235   1681640 |
     +---------------------------------------------------------------------+

Use scan and reshape with matrix :使用scan和重塑matrix

s <- "2622731,1387660,2621628,1444522,2619235,1681640" # test data

matrix(scan(text = s, sep = ",", quiet = TRUE), ncol = 2, byrow = TRUE)
##         [,1]    [,2]
## [1,] 2622731 1387660
## [2,] 2621628 1444522
## [3,] 2619235 1681640

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM