R编程中的错误:需要帮助来修复它。

时间:2021-12-04 02:05:03
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")

The above has only six columns but it identifies it as 11, because some column names have space. [How to fix this]

上面的列只有6列,但它将它标识为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)

1 个解决方案

#1


4  

There are a couple of things you can do. Manually, you can add quotes around those names that contain multiple words.

你可以做一些事情。手动地,您可以在包含多个单词的名称周围添加引号。

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

Note that check.names = FALSE is optional and will probably make the work more difficult because you will need to backtick the names. But the advantage there is that the data is displayed exactly as it was entered.

请注意检查。name = FALSE是可选的,可能会使工作变得更加困难,因为您需要在名称后面加上引号。但是,这里的优点是数据在输入时完全显示出来。

Programmatically (and probably the recommended thing to do), you can remove the spaces between the words first, then read in the data. I used the original text for this following part.

通过编程(可能也是推荐的做法),您可以先删除单词之间的空格,然后再读取数据。以下部分我使用了原文。

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

#1


4  

There are a couple of things you can do. Manually, you can add quotes around those names that contain multiple words.

你可以做一些事情。手动地,您可以在包含多个单词的名称周围添加引号。

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

Note that check.names = FALSE is optional and will probably make the work more difficult because you will need to backtick the names. But the advantage there is that the data is displayed exactly as it was entered.

请注意检查。name = FALSE是可选的,可能会使工作变得更加困难,因为您需要在名称后面加上引号。但是,这里的优点是数据在输入时完全显示出来。

Programmatically (and probably the recommended thing to do), you can remove the spaces between the words first, then read in the data. I used the original text for this following part.

通过编程(可能也是推荐的做法),您可以先删除单词之间的空格,然后再读取数据。以下部分我使用了原文。

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