This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Assigning Objects To A Variable

Hi all,

This is perhaps very basic to the R pros, but I was wondering how I could assign a variable, say "control" to a string of sorts. For example:

"musclenormal15494"
"musclenormal15497"
"musclenormal15501"

My interest is in the muscle_normal part and since it's quite a list, it will be quite hectic to assign each one individually.

Any suggestions will be greatly appreciated.

Thanks.

-Avoks

r

Hi, Sorry its unclear to me, do you have list with strings given above stored in it, and you want to filter the ones starting with muscle_normal and store it in other list!!

Hi Sukhdeep. These are gse18732 data I extracted from GEO using getGEO. I'm running a limma analysis and trying to assign the different cases to the 3 test levels, T2D, IGT and Control(normal) so that I can create an appropriate cont. matrix. I usually use a different "channel" but for this particular study the channels are quite confusing and the "title" seemed to be the most straightforward but it comes with the numbers, hence my question.

1 answer

I guess what you want is:

x=c("muscle_normal_15494","muscle_normal_15497","muscle_normal_15501","the_other_string_1","the_other_string_2")
myfactors=rep(NA,length(x))
myfactors[grep("muscle_normal",x)]="control"
myfactors[grep("the_other_string",x)]="other"
myfactors=as.factor(myfactors)

Thanks Leonor. But that's not really what I want, I think, because this code involves manually inputting each one to create a list...that's what I'm trying to avoid.

This is just an example, you absolutely should not need to enter these by hand! If this is stored in a file, you can of course read the file with x=read.table("myfile"), if it is a list, you just have to unlist() it first. How is your data stored?

Actually now I read through again, I kind of get it. I however got the info I wanted by sorting and extracting by numbers:

groups = pData(phenoData(gsexxxxxdat[[1]]))$title

groups=as.character(groups)

groups=sort(groups)

groups[c(1:45)]="T2D"

groups[c(46:71)]="IGT"

groups[c(72:118)]="control"

f = factor(groups, levels=c("control","T2D","IGT"))

Perhaps not the most intuitive way but I got what I wanted:).

Thanks again.

Log in to answer this question.