0

3 hours spending trying to understand what happened on my files

WRONG WAY : a subset on subset

DataA=subset(Data,Data$var1=="1")
DataA$newvar=Results$cluster
DataB=subset(DataA,Data$newvar=="7")

GOOD WAY : one subset per object

DataA_var1=subset(Data,Data$var1=="1")
DataA_var1$newvar=Results$cluster
DataB=subset(DataA_var1,Data$newvar=="7")

I read a lot on $ operator is invalid for atomic vectors This error was also linked to "incorrect number of dimensions" in my case (I tried the trick as.data.frame()) I re installed last RStudio version.

My point is : if you have a $ operator is invalid for atomic vectors error, this is not necessary a typeof() issue... Try to see if you used twice subset() on the same object.

rawr
  • 20,481
  • 4
  • 44
  • 78
Xavyest
  • 11
  • 1
  • 1
  • 2
    Inside the `subset`, you don't need `Data$`. The `$ operator ...` message you will get when you are using it on a `matrix` object. Please check the `str(Data)` – akrun Feb 22 '16 at 11:30
  • 1
    I really don't get what your question is about. The two blocks (good and bad) looks the same to me, except for the name of an object. As @akrun mentioned, your use of `subset` is flawed. What are you trying to achieve? Please, share also some data and an expected output, so you could receive some useful help. – nicola Feb 22 '16 at 11:39
  • 1
    Please provide a minimal, complete, and reproducible example that anyone else can simply copy and paste into their R session to run. All library statements and inputs need to be included. Cut down your data to the minimum needed to illustrate the problem if it's large and if your input is `x` then show it by displaying the output of `dput(x)` in your question. See [mcve] for general advice and see [How to Make a Great R Reproducible Example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more R oriented advice on how to ask a question on SO. – G. Grothendieck Feb 22 '16 at 11:53
  • @akrun yes you're right, generally speaking one gets this message when trying to use $ on a matrix... but all ths objects I'm speaking about are 'data.frame' – Xavyest Feb 22 '16 at 13:20

1 Answers1

1

You could try something like:

DataA <- subset(Data, var1 == "1")
DataA$newvar <- Results$cluster
DataB <- subset(DataA, newvar == "7")

The first line should be read as "DataA gets a subset of Data where var1 equals 1."

Just for syntax purposes, I would suggest "<-" instead of "=" for R purposes.

el_dewey
  • 97
  • 10