R read.csv More columns than column names error

R read.csv More columns than column names error

Questions : R read.csv More columns than column names error

1406

I have a problem when importing .csv file in4codes_import into R. With my code:

t <- read.csv("C:\N0_07312014.CSV", na.string=c("","null","NaN","X"), header=T, stringsAsFactors=FALSE,check.names=F) 

R reports an error and does not do what I in4codes_import want:

Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names 

I guess the problem is because my data is in4codes_import not well formatted. I only need data from in4codes_import [,1:32]. All others should be deleted.

Data can be downloaded in4codes_import from: https://drive.google.com/file/d/0B86_a8ltyoL3VXJYM3NVdmNPMUU/edit?usp=sharing

Thanks so much!

Total Answers 8
30

Answers 1 : of R read.csv More columns than column names error

That’s one wonky CSV file. Multiple in4codes_import headers tossed about (try pasting it to in4codes_import CSV Fingerprint) to see what I mean.

Since I don’t know the data, it’s in4codes_import impossible to be sure the following in4codes_import produces accurate results for you, but in4codes_import it involves using readLines and other R in4codes_import functions to pre-process the text:

# use readLines to get the data dat <- readLines("N0_07312014.CSV") # i had to do this to fix grep errors Sys.setlocale('LC_ALL','C') # filter out the repeating, and wonky headers dat_2 <- grep("Node Name,RTC_date", dat, invert=TRUE, value=TRUE) # turn that vector into a text connection for read.csv dat_3 <- read.csv(textConnection(paste0(dat_2, collapse="n")), header=FALSE, stringsAsFactors=FALSE) str(dat_3) ## 'data.frame': 308 obs. of 37 variables: ## $ V1 : chr "Node 0" "Node 0" "Node 0" "Node 0" ... ## $ V2 : chr "07/31/2014" "07/31/2014" "07/31/2014" "07/31/2014" ... ## $ V3 : chr "08:58:18" "08:59:22" "08:59:37" "09:00:06" ... ## $ V4 : chr "" "" "" "" ... ## .. more ## $ V36: chr "" "" "" "" ... ## $ V37: chr "0" "0" "0" "0" ... # grab the headers headers <- strsplit(dat[1], ",")[[1]] # how many of them are there? length(headers) ## [1] 32 # limit it to the 32 columns you want (Which matches) dat_4 <- dat_3[,1:32] # and add the headers colnames(dat_4) <- headers str(dat_4) ## 'data.frame': 308 obs. of 32 variables: ## $ Node Name : chr "Node 0" "Node 0" "Node 0" "Node 0" ... ## $ RTC_date : chr "07/31/2014" "07/31/2014" "07/31/2014" "07/31/2014" ... ## $ RTC_time : chr "08:58:18" "08:59:22" "08:59:37" "09:00:06" ... ## $ N1 Bat (VDC) : chr "" "" "" "" ... ## $ N1 Shinyei (ug/m3): chr "" "" "0.23" "null" ... ## $ N1 CC (ppb) : chr "" "" "null" "null" ... ## $ N1 Aeroq (ppm) : chr "" "" "null" "null" ... ## ... continues 

0

4

Answers 2 : of R read.csv More columns than column names error

Open the .csv as a text file (for in4codes_import example, use TextEdit on a Mac) and in4codes_import check to see if columns are being in4codes_import separated with commas.

csv is “comma separated vectors”. For in4codes_import some reason when Excel saves my csv’s it in4codes_import uses semicolons instead.

When opening your csv use:

read.csv("file_name.csv",sep=";") 

Semi colon is just an example but as in4codes_import someone else previously suggested don’t in4codes_import assume that because your csv looks good in4codes_import in Excel that it’s so.

0

3

Answers 3 : of R read.csv More columns than column names error

If you only need the first 32 columns, in4codes_import and you know how many columns there are, in4codes_import you can set the other columns classes to in4codes_import NULL.

read.csv("C:\N0_07312014.CSV", na.string=c("","null","NaN","X"), header=T, stringsAsFactors=FALSE, colClasses=c(rep("character",32),rep("NULL",10))) 

If you do not want to code up each in4codes_import colClass and you like the guesses in4codes_import read.csv then just save that csv and in4codes_import open it again.

Alternatively, you can skip the header in4codes_import and name the columns yourself and remove in4codes_import the misbehaved rows.

A<-data.frame(read.csv("N0_07312014.CSV", header=F,stringsAsFactors=FALSE, colClasses=c(rep("character",32),rep("NULL",5)), na.string=c("","null","NaN","X"))) Yournames<-as.character(A[1,]) names(A)<-Yournames yourdata<-unique(A)[-1,] 

The code above assumes you do not want in4codes_import any duplicate rows. You can in4codes_import alternatively remove rows that have the in4codes_import first entry equal to the first column in4codes_import name, but I’ll leave that to you.

0

6

Answers 4 : of R read.csv More columns than column names error

try read.table() instead of read.csv()

0

6

Answers 5 : of R read.csv More columns than column names error

I was also facing the same issue. Now in4codes_import solved.

Just use header = FALSE

read.csv("data.csv", header = FALSE) -> mydata 

0

2

Answers 6 : of R read.csv More columns than column names error

I had the same problem. I opened my data in4codes_import in textfile and double expressions are in4codes_import separated by semicolons, you should in4codes_import replace them with a period

0

1

Answers 7 : of R read.csv More columns than column names error

I was having this error that was caused in4codes_import by multiple rows of meta data at the top in4codes_import of the file. I was able to use read.csv in4codes_import by doing skip= and skipping those rows.

data <- read.csv(‘/blah.csv’,skip=3)

0

1

Answers 8 : of R read.csv More columns than column names error

For me, the solution was using csv2 in4codes_import instead of csv.

0

Answer Link
jidam
  • Unable to run NoraUI mvn verify goal
  • Unable to run my app on emulator in VS Code
  • Unable to run multiple instances of libVLC(MobileVLCKit) in IOS via flutter framework
  • Unable to run make on griddb source on ubuntu 20.04 (building from source)
  • Unable to run latexindent macOS Monterey 12.0.1
  • Unable to run kotlinc-native command
  • Unable to run JUnit Test… Java.lang.ExceptionInInitializerError (Android Studio)
  • Unable to run java with -Xmx > 966m
  • Unable to run ionic cap run android from wsl2 inorder to start android emulator
  • Unable to run Intel HAXM installer: Cannot start process, the working directory does not exist
  • fs
  • Unable to run Google Analytics sample code
  • unable to run flutter run after upgarding to flutter 2.8.0 from 2.5.3
  • Unable to run Django with PostgreSQL in Docker
  • Unable to Run Container Using testcontainers
  • Unable to run ClojureScript Hello World program, Error building classpath. Error reading edn.
  • unable to run client command for apache karaf 4.3.3 through remote server
  • Unable to run c program 2nd time using eclipse
  • unable to run c++ in visual studio code on m1 chipset
  • Unable to run Android Instrumented Tests
  • Unable to run adb, check your Android SDK installation and ANDROID_SDK_ROOT environment variable: …AndroidSdkplatform-toolsadb.exe
  • Unable to run a singlespecific .spec.ts file through angular cli using ng test –include option
  • Unable to run a Mango query
  • Unable to return response back to view in laravel from package
  • Unable to return object reference in std::optional
  • Unable to return NULL in a function that expects an integer return type
  • Unable to return correct change in JavaScript Cash Register
  • Unable to retrieve version information from Elasticsearch nodes. Request timed out
  • Unable to retrieve values from Axios Response data
  • Unable to retrieve dotenv JWT secret Error: secretOrPrivateKey must have a value
  • Unable to resolve your shell environment
  • Unable to resolve token for FCM while implementing Push notification for Xamarin
  • Unable to resolve the request yii
  • Unable to resolve service for type Swashbuckle.AspNetCore.Swagger.ISwaggerProvider
  • Unable to resolve service for type Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger