0

I just imported a csv file into mongodb by using "mongoimport.exe" like below: 1 Now I wanna delete a column named "The Arts Centre" from a collection in R, When I try this:

>mongo.get.database.collections(mongo.db) 
[1] "assi3.2013_4"
>coll <- "assi3.2013_4"

> str(coll)
chr "assi3.2013_4"

coll$The_Arts_Centre <- NULL

there is a warning:

Warning message:
In coll$The_Arts_Centre <- NULL : Coercing LHS to a list

Could someone help me to solve this ?

Felix Sun
  • 1
  • 3
  • 1
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). What is the `class()` and strucutre (`str()`) of `coll`? – MrFlick May 30 '15 at 03:03
  • I imported .csv file to mongodb, and put .csv to a collection. `>mongo.get.database.collections(mongo.db) [1] "assi3.2013_4" >coll <- "assi3.2013_4" > str(coll) chr "assi3.2013_4" > mongo.get.database.collections(mongo,db) [1] "assi3.2013_4" > coll <- "assi3.2013_4"` "assi3.2013_4" contains a table with 720 rows and 30 columns, and I just need the first three columns and wanna delete last 27 columns. Please give me a hand. – Felix Sun May 30 '15 at 03:31
  • You say: `a column name is "The Arts Centre"`. But R does not allow spaces in tokens. If you really have a column name with spaces then you need to issue this command: `coll$'The_Arts_Centre' <- NULL` which has quotes or back-ticks around the column name. Underscores are not parsed like spaces by the R parsing engine. (Added the [mongodb] tag since this may not actually be about the R language.) – IRTFM May 30 '15 at 03:38
  • But when I try `mongo.find.one(mongo,coll)` after `coll$'The_Arts_Centre' <- NULL` , there is a new error shows `Error in mongo.find.one(mongo, coll) STRING_ELT() can only be applied to a 'character vector', not a 'list'` . What is the matter? – Felix Sun May 30 '15 at 03:44
  • Felix, as MrFlick asked, can you edit your question with the results from `str(coll)` please – user20650 May 30 '15 at 04:07

1 Answers1

0

A warning isn't an error; a warning tells you something is happening, but the operation finishes anyway. In this case, it's telling you that the data structure on the left hand side (LHS) of your assignment (<-) is being coerced to a list, and is no longer a data.frame or whatever it was before.

Without more information about how your data is structured in the first place, the only way to avoid the warning is to subset by what you want instead of what you don't, which gives you more control of the output format, e.g. by using an explicit coercion (as.character, as.numeric, as.data.frame, etc.).

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • Exactly, when I try `mongo.find.one(mongo,coll)` after `coll$'The_Arts_Centre' <- NULL` , there is a new error shows `Error in mongo.find.one(mongo, coll) STRING_ELT() can only be applied to a 'character vector', not a 'list'` . So how can I do the deletion of drop a column from a collection ? – Felix Sun May 30 '15 at 03:45
  • There's more going on here than I assumed. It sounds like more of a Mongo DB question than an R question, honestly. Subsetting in R is easy; you can just do `coll[,c(1:3)]` _if_ `coll` is a `data.frame` or similar. You need to edit your post with more of the relevant code and setup, because you're apparently getting an R error to a Mongo problem. – alistaire May 30 '15 at 04:43
  • I tried `coll[, c(1:3)]` , it still contains a error: `Error in coll[, c(1:3)] : incorrect number of dimensions`. Am I wrong with Mongo ? I insert one picture in my edited post. – Felix Sun May 30 '15 at 05:52
  • Got it: All Mongo is giving you from your `get` command is the string `"assi3.2013_4"`. It's giving you a warning because there's no column to remove. – alistaire May 31 '15 at 00:02