vtammy
vtammy

Reputation: 125

Error in R Programming: Need help to fix it

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]

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)

Upvotes: 4

Views: 91

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99371

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.

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

Upvotes: 5

Related Questions