我有这样的样本数据
library(tidyverse)
library(lubridate)
set.seed(88)
start <- ymd("2009/11/01")
data <- data_frame(date = start + months(0:11)
, donuts = rnorm(12, mean = 30, sd = 5) %>% round(., 0)
, year = year(date)
, month = month(date, label =T, abbr =T)
)
ggplot(data = data, aes(x = date, y = donuts)) + geom_line()
我需要根据SPSS代码中的固定位置读取单个数据,给出如下所示的colume名称和数据位置(片段代码)
00013 326 10 5143 1 1 11 1 73311333111111111111511111111111111111511111111111111111111111111111111111111111111111111111111 3 3 875421212454542534524542133452421245443454234234544452124445454
00023 325 10 5143 1 1 11 1 74 1333111111111113111111111111111113111111111111111111111111111111111111111111111111111111111 864212322454542435434542143342412145443434324235454454542434543
我写过R代码,
ID 1-4
QUARTER 5
SEX 7
AGEA 8
AGEB 9
RACE 10
OCCA 11
OCCB 12
LOCATION 13
REGION 14
EDUCATION 15
HHIA 16
HHIB 17
PERSONALINCOME 18
ENGLIT 19
CHILIT 20
RUMILIT 21
JAWILIT 22
TAMILLIT 23
MORELIT 24
ILLITERATE 25
MRO1 170
MRO2 171
MRO3 172
MRO4 173
MRO5 174
是否有更简单的方法来使用SPSS代码中指示的位置,而不是逐个指定宽度。
答案 0 :(得分:0)
好的,我发现它使用read_fwf函数
#read data
library(data.table)
library(readr)
mydata1 <- data.table(read_fwf("MY/Q315-Q216.txt", fwf_positions(
c(5,9,21:39,176:180),
c(8,9,21:39,176:180),
c("ID",
"QUARTER",
"SEX",
"AGEA",
"AGEB",
"RACE",
"OCCA",
"OCCB",
"LOCATION",
"REGION",
"EDUCATION",
"HHIA",
"HHIB",
"PERSONALINCOME",
"ENGLIT",
"CHILIT",
"RUMILIT",
"JAWILIT",
"TAMILLIT",
"MORELIT",
"ILLITERATE",
"MRO1",
"MRO2",
"MRO3",
"MRO4",
"MRO5")))