如何解决将逗号分隔的整数长字符串转换为x和y列
我的数据是一排很长的单行值,用逗号分隔,其中每个其他值都是x或y坐标
数据如下所示: 2622731,1387660,2621628,1444522,2619235,1681640
但是我希望它看起来像这样:
2622731,1387660
2621628,1444522
2619235,1681640
像上面的示例一样,要遍历整个文件并删除逗号并按Enter键,我该如何在R(或Stata)中自动化呢?
解决方法
在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
那时,也许
data = as.data.frame(data)
names(data) = c("x","y")
# x y
# 1 2622731 1387660
# 2 2621628 1444522
# 3 2619235 1681640
,
在Stata中,可接受的R解的类似物可能涉及split
和reshape long
。这是另一种方法:
* data example
clear
set obs 1
gen strL data = "2622731,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 |
+---------------------------------------------------------------------+
,
使用scan
并用matrix
重塑:
s <- "2622731,1681640" # test data
matrix(scan(text = s,sep = ",quiet = TRUE),byrow = TRUE)
## [,2]
## [1,] 2622731 1387660
## [2,] 2621628 1444522
## [3,] 2619235 1681640
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。