m <- read.table(row.names=1, header=TRUE, text=
" Assignment Caused by CI Quality Indicator Update Operator Update Description Update
Assignment 0.0 0.0 0.49 0.0 0.0 0.0
Caused by CI 0.0 0.0 0.0 0.0 0.0 0.0
Quality Indicator 0.0 0.75 0.0 0.0 0.0 0.0
Update 0.0 0.0 0.0 0.0 0.0 0.0
Operator Update 0.0 0.0 0.0 0.0 0.0 0.0
Description Update 0.0 0.0 0.0 0.0 0.0 0.0")
上面只有六列,但它将其标识为11,因为某些列名称有空格。 [如何解决这个问题]
Error Message: Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 1 did not have 11 elements
m <-as.matrix(m)
答案 0 :(得分:4)
你可以做几件事。手动,您可以在包含多个单词的名称周围添加引号。
text <- "Assignment 'Caused by CI' 'Quality Indicator' Update 'Operator Update' 'Description Update'
Assignment 0.0 0.0 0.49 0.0 0.0 0.0
'Caused by CI' 0.0 0.0 0.0 0.0 0.0 0.0
'Quality Indicator' 0.0 0.75 0.0 0.0 0.0 0.0
Update 0.0 0.0 0.0 0.0 0.0 0.0
'Operator Update' 0.0 0.0 0.0 0.0 0.0 0.0
'Description Update' 0.0 0.0 0.0 0.0 0.0 0.0"
read.table(row.names = 1, header = TRUE, text = text, check.names = FALSE)
# Assignment Caused by CI Quality Indicator Update Operator Update Description Update
# Assignment 0 0.00 0.49 0 0 0
# Caused by CI 0 0.00 0.00 0 0 0
# Quality Indicator 0 0.75 0.00 0 0 0
# Update 0 0.00 0.00 0 0 0
# Operator Update 0 0.00 0.00 0 0 0
# Description Update 0 0.00 0.00 0 0 0
请注意,check.names = FALSE
是可选的,可能会使工作更加困难,因为您需要对名称进行反引号。但优点是数据的输出与输入的完全一致。
以编程方式(可能是推荐的事情),您可以先删除单词之间的空格,然后读入数据。我在以下部分使用了原始文本。
text <- " Assignment Caused by CI Quality Indicator Update Operator Update Description Update
Assignment 0.0 0.0 0.49 0.0 0.0 0.0
Caused by CI 0.0 0.0 0.0 0.0 0.0 0.0
Quality Indicator 0.0 0.75 0.0 0.0 0.0 0.0
Update 0.0 0.0 0.0 0.0 0.0 0.0
Operator Update 0.0 0.0 0.0 0.0 0.0 0.0
Description Update 0.0 0.0 0.0 0.0 0.0 0.0"
read.table(text = gsub("([A-Za-z]) ([A-Za-z])", "\\1\\2", text))
# Assignment CausedbyCI QualityIndicator Update OperatorUpdate DescriptionUpdate
# Assignment 0 0.00 0.49 0 0 0
# CausedbyCI 0 0.00 0.00 0 0 0
# QualityIndicator 0 0.75 0.00 0 0 0
# Update 0 0.00 0.00 0 0 0
# OperatorUpdate 0 0.00 0.00 0 0 0
# DescriptionUpdate 0 0.00 0.00 0 0 0