我有一个文本文件,行中包含不相等的元素。有时第二列包含数据,有时它包含NA
,有时根本没有记录。我知道如果一行中只有4个元素,我应该在第二列中插入NA
作为元素。但是,我不知道该怎么做。以下是一个示例数据集:
abc.def ghi.jkl mno pqr A*
bc.def NA no qr A
c-e.ef non qrr AE
fg.gg no qr E
aa.bb cc.dd ee ff A*
这是期望的结果:
desired.result <- read.table(text = '
Name1 Name2 Name3 Name4 Status
abc.def ghi.jkl mno pqr A*
bc.def NA no qr A
c-e.ef NA non qrr AE
fg.gg NA no qr E
aa.bb cc.dd ee ff A*
', header = TRUE)
我还没有走得太远,但我已经能够分割数据并使用以下代码将其输入matrix
。当然,数据不对齐。
setwd('c:/users/mmiller21/simple R programs')
my.data <- readLines('name_data.txt')
matrix(unlist(strsplit(unlist(my.data), " ")), ncol=5, byrow=TRUE)
# [,1] [,2] [,3] [,4] [,5]
# [1,] "abc.def" "ghi.jkl" "mno" "pqr" "A*"
# [2,] "bc.def" "NA" "no" "qr" "A"
# [3,] "c-e.ef" "non" "qrr" "AE" "fg.gg"
# [4,] "no" "qr" "E" "aa.bb" "cc.dd"
# [5,] "ee" "ff" "A*" "abc.def" "ghi.jkl"
不知何故,我应该在使用strsplit(unlist(my.data), " ")
之后计算元素的数量,然后在仅包含四个元素的每一行中插入NA
作为第二个元素。然后将数据输入矩阵。感谢您对此的任何帮助。我更喜欢基地R。
答案 0 :(得分:3)
dat <- read.table(text="abc.def ghi.jkl mno pqr A*
bc.def NA no qr A
c-e.ef non qrr AE
fg.gg no qr E
aa.bb cc.dd ee ff A*", fill=TRUE, stringsAsFactors=FALSE)
names(dat) <- c('Name1' , 'Name2', 'Name3', 'Name4','Status')
is.na(dat[[5]]) <- dat[[5]]=="" # set blanks in col 5 to NA
t( apply( dat, 1, function(r) if( is.na( r[5]) ) {r[c(1,5,2:4)]}else {r}))
#---------
[,1] [,2] [,3] [,4] [,5]
[1,] "abc.def" "ghi.jkl" "mno" "pqr" "A*"
[2,] "bc.def" NA "no" "qr" "A"
[3,] "c-e.ef" NA "non" "qrr" "AE"
[4,] "fg.gg" NA "no" "qr" "E"
[5,] "aa.bb" "cc.dd" "ee" "ff" "A*"
答案 1 :(得分:2)
将dat
替换为您的文件名:
dat <- textConnection("abc.def ghi.jkl mno pqr A*
bc.def NA no qr A
c-e.ef non qrr AE
fg.gg no qr E
aa.bb cc.dd ee ff A*")
my.lines <- readLines(dat)
my.rows <- strsplit(my.lines, " ")
adjust <- function(row) {
if (length(row) == 4) c(head(row, 1), NA, tail(row, 3))
else row
}
my.fixed <- lapply(my.rows, adjust)
out <- matrix(unlist(my.fixed), ncol = 5, byrow = TRUE)
out[out == "NA"] <- NA
答案 2 :(得分:2)
您可以使用选项fill=TRUE
,然后翻译错过的行:
dat <- read.table(text='abc.def ghi.jkl mno pqr A*
bc.def NA no qr A
c-e.ef non qrr AE
fg.gg no qr E
aa.bb cc.dd ee ff A*',fill=TRUE)
t(apply(dat,1,function(x){
if(nchar(x[5])==0)
x= c(x[1],NA_character_,x[2:4])
x
}))
[,1] [,2] [,3] [,4] [,5]
[1,] "abc.def" "ghi.jkl" "mno" "pqr" "A*"
[2,] "bc.def" NA "no" "qr" "A"
[3,] "c-e.ef" NA "non" "qrr" "AE"
[4,] "fg.gg" NA "no" "qr" "E"
[5,] "aa.bb" "cc.dd" "ee" "ff" "A*"
答案 3 :(得分:1)
读取行,按空格字符拆分,并附加NA:
txt <- readLines(file)
t(sapply(strsplit(txt, "\\s+"), function(x) if(length(x) < 5) append(x, NA, 1) else x))
# [,1] [,2] [,3] [,4] [,5]
# [1,] "abc.def" "ghi.jkl" "mno" "pqr" "A*"
# [2,] "bc.def" "NA" "no" "qr" "A"
# [3,] "c-e.ef" NA "non" "qrr" "AE"
# [4,] "fg.gg" NA "no" "qr" "E"
# [5,] "aa.bb" "cc.dd" "ee" "ff" "A*"
包含数据管理的完整版:
file <- tempfile()
cat("abc.def ghi.jkl mno pqr A*
bc.def NA no qr A
c-e.ef non qrr AE
fg.gg no qr E
aa.bb cc.dd ee ff A*", "\n", sep="", file=file)
txt <- readLines(file)
t(sapply(strsplit(txt, "\\s+"), function(x) if(length(x) < 5) append(x, NA, 1) else x))
unlink(file)
请注意,这与@Flodel
类似