user2806363
user2806363

Reputation: 2593

Subsetting data in R

I'm trying to get subset of my data in R by row names, but I'm getting flowing error.

mydata is my main data file and I want just get subset of it by row names.

what I tried is :

data<-mydata[c("hsa-miR-150"   , "hsa-miR-16"    , "hsa-miR-192"  , ..`enter code here`.. )]

and I got : error: unexpected string constant in "sa-miR-16" , "hsa-miR-192" ,.... also I tried this :

 attributes(Arow)
$levels
 [1] "hsa-miR-150"    "hsa-miR-16"     "hsa-miR-192"    "hsa-miR-194"    "hsa-miR-19a"   
 [6] "hsa-miR-19b"    "hsa-miR-22" 
> p<-matrix(nrow=16, ncol=1)
> p<-matrix(nrow=16, ncol=1, Arow)
> y<-mydata[p]
> dim(y)
>NULL

moreover, even this simple code didn't work for me !:

 y<-c("hsa-miR-150"   , "hsa-miR-16"    , "hsa-miR-192"   , "hsa-miR-194"   , "hsa-miR-19a", "hsa-miR-19b"   , "hsa-miR-22"    , "hsa-miR-223"   , "hsa-miR-23a"    ,"hsa-miR-23b" ,"hsa-miR-25"    , "hsa-miR-30b"    ,"hsa-miR-30c"   , "hsa-miR-425"   , "hsa-miR-486-5p" "hsa-miR-92a" )
Error: unexpected string constant in "sa-miR-16"    , "hsa-miR-192"   , "hsa-miR-194"   , "hsa-miR-19a", 

Can somebody tell me, what's going wrong here?! and what should I do ?

Upvotes: 0

Views: 298

Answers (2)

zero323
zero323

Reputation: 330353

Start with reading this: How to make a great R reproducible example?.

Then remove enter code here and add comma after rows list:

data <- mydata[c("hsa-miR-150", "hsa-miR-16", "hsa-miR-192"), ]

Then you can add missing comma here:

y <- c("hsa-miR-150", "hsa-miR-16", "hsa-miR-192", "hsa-miR-194",
       "hsa-miR-19a", "hsa-miR-19b", "hsa-miR-22", "hsa-miR-223",
       "hsa-miR-23a", "hsa-miR-23b", "hsa-miR-25", "hsa-miR-30b",
       "hsa-miR-30c", "hsa-miR-425", "hsa-miR-486-5p", "hsa-miR-92a" )

Upvotes: 1

Paul Hiemstra
Paul Hiemstra

Reputation: 60984

This error in general relates to missing syntax, e.g. a comma or an =. Your final example is missing a comma, the last two items are not separated by a comma. This works:

y<-c("hsa-miR-150"   , "hsa-miR-16"    , "hsa-miR-192"   , "hsa-miR-194"   , "hsa-miR-19a", "hsa-miR-19b"   , "hsa-miR-22"    , "hsa-miR-223"   , "hsa-miR-23a"    ,"hsa-miR-23b" ,"hsa-miR-25"    , "hsa-miR-30b"    ,"hsa-miR-30c"   , "hsa-miR-425"   , "hsa-miR-486-5p", "hsa-miR-92a" )

R, like any programming language, is a formal language where is very important to use the correct syntax. So, when you write code, check carefully if you have used the correct syntax.

Upvotes: 1

Related Questions