An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120811/198ed079/attachment.pl>
choosing multiple columns
7 messages · John Kane, Ista Zahn, Sachinthaka Abeywardana +2 more
mydata[ , 1:8] Or let's say you only one the 4th, 6th and 8th columns mydata[ , c(4,6,8)] and so on. There are several good intro's available on the R website that will walk you through this type of thing. If you're a recovering SAS or SPSS user this paper may be of real help www.et.bs.ehu.es/~etptupaf/pub/R/RforSAS&SPSSusers.pdf John Kane Kingston ON Canada
-----Original Message----- From: sachin.abeywardana at gmail.com Sent: Sat, 11 Aug 2012 21:59:59 +1000 To: r-help at r-project.org Subject: [R] choosing multiple columns Hi all, I have a data frame that has the columns OFB1, OFB2, OFB3,... OFB10. How do I select the first 8 columns efficiently without typing each and every one of them. i.e. I want something like: a<-data.frame(initial_data$OFB1-10) #i know this is wrong, what would be the correct syntax? Thanks, Sachin [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
Hi Sachin,
There are at least two ways. The safer way is to use a regular
expression to find the matching columns, like this:
a <- initial_data[grep("^OFB[0-9]+", names(initial_data))]
Alternatively, if you know that the columns you want are the first 8
you can select them by position, like this:
a <- initial_data[1:8]
Best,
Ista
On Sat, Aug 11, 2012 at 7:59 AM, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote:
Hi all,
I have a data frame that has the columns OFB1, OFB2, OFB3,... OFB10.
How do I select the first 8 columns efficiently without typing each and
every one of them. i.e. I want something like:
a<-data.frame(initial_data$OFB1-10) #i know this is wrong, what would be
the correct syntax?
Thanks,
Sachin
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120811/bb9b8819/attachment.pl>
On Sat, Aug 11, 2012 at 8:51 AM, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote:
I should have mentioned that I do not know the number index of the columns, but regardless, thanks for the responses
Right, so use my first method. This does not depend on the position of the columns. Best, Ista
On Sat, Aug 11, 2012 at 10:46 PM, Ista Zahn <istazahn at gmail.com> wrote:
Hi Sachin,
There are at least two ways. The safer way is to use a regular
expression to find the matching columns, like this:
a <- initial_data[grep("^OFB[0-9]+", names(initial_data))]
Alternatively, if you know that the columns you want are the first 8
you can select them by position, like this:
a <- initial_data[1:8]
Best,
Ista
On Sat, Aug 11, 2012 at 7:59 AM, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote:
Hi all,
I have a data frame that has the columns OFB1, OFB2, OFB3,... OFB10.
How do I select the first 8 columns efficiently without typing each and
every one of them. i.e. I want something like:
a<-data.frame(initial_data$OFB1-10) #i know this is wrong, what would be
the correct syntax?
Thanks,
Sachin
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
HI,
Try this:
dat1<-as.data.frame(matrix(rnorm(50,5),ncol=10))
colnames(dat1)<-paste0("OFB",1:10)
#to select first 8 columns - easy method
dat1[,1:8]
#2nd method
wanted<-paste0("OFB",1:8)
dat1[,colnames(dat1)%in%wanted]
#3rd method
#regular expression to select 3rd, 5th columns
dat1[grep("[[:alnum:]][c(3,5)]",colnames(dat1))]
#???? OFB3???? OFB5
#1 6.378474 7.490392
#2 5.323282 4.728561
#3 5.415081 4.661548
#4 4.000541 5.286831
#5 3.598919 6.080370
dat2<-data.frame(dat1,CFB=rnorm(5,15))
#select columns having OFB as column name
dat2[grep("OFB",colnames(dat2))]
#select 4-8 columns
dat2[grep("[4-8]",colnames(dat2))]
?# ?? OFB4???? OFB5???? OFB6???? OFB7???? OFB8
#1 4.545049 7.490392 5.441275 3.433050 4.656184
#2 5.015531 4.728561 5.429073 5.268677 5.569176
#3 5.533485 4.661548 5.586189 4.694112 5.209213
#4 6.427448 5.286831 5.521572 4.036457 5.532234
#5 5.500054 6.080370 6.259925 3.946102 4.554102
Hope this helps.
A.K.
----- Original Message -----
From: Sachinthaka Abeywardana <sachin.abeywardana at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Saturday, August 11, 2012 7:59 AM
Subject: [R] choosing multiple columns
Hi all,
I have a data frame that has the columns OFB1, OFB2, OFB3,... OFB10.
How do I select the first 8 columns efficiently without typing each and
every one of them. i.e. I want something like:
a<-data.frame(initial_data$OFB1-10) #i know this is wrong, what would be
the correct syntax?
Thanks,
Sachin
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
1 day later
On Aug 11, 2012, at 6:01 AM, Ista Zahn wrote:
On Sat, Aug 11, 2012 at 8:51 AM, Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> wrote:
I should have mentioned that I do not know the number index of the columns, but regardless, thanks for the responses
Right, so use my first method. This does not depend on the position of the columns.
I would counsel greater consideration of the possible ranges of the
column names. Even using a variation on Ista Zahn's method intended to
deliver on the first 8 will fail if the range of possible values is
greater than 10 in number or the numbers do not start from 1.
If the numbers of the columns do start from 1, you could try this
grep("^OFB[1-8]", paste0("OFB", 1:100) , value=TRUE )[1:8]
Otherwise consider these efforts;
> set.seed(123); test <- sample( paste0("OFB", 1:100), 20)
> sort(test)[1:8]
[1] "OFB21" "OFB27" "OFB29" "OFB4" "OFB41" "OFB42" "OFB5" "OFB50
> grep("^OFB[1-8]", test , value=TRUE )[1:8]
[1] "OFB29" "OFB79" "OFB41" "OFB86" "OFB5" "OFB50" "OFB83" "OFB51"
Note that even this does not get what you want which is =
> test[order(as.numeric( sub("OFB", "", test)))][1:8]
[1] "OFB4" "OFB5" "OFB9" "OFB21" "OFB27" "OFB29" "OFB41" "OFB42"
There is also a function named mixedsort in Greg Warnes package gtools
which automatically splits the alpha and numeric components of of an
alphanumeric vector and then orders by the two of them separately.
Something like this might achieve:
> test[ order( sub("[0-9]+","", test), # an alpha sort .. followed
by numeric sort
as.numeric(gsub("[[:alpha:]]*([[:digit:]]*)", '\\1',
test) ) )]
[1] "OFB4" "OFB5" "OFB9" "OFB21" "OFB27" "OFB29" "OFB41" "OFB42"
"OFB50" "OFB51" "OFB60" "OFB77" "OFB78"
[14] "OFB79" "OFB83" "OFB86" "OFB87" "OFB91" "OFB94" "OFB98"
gtools::ixedsort is based on gtools::mixedorder and has more
sophistication, for instance the attempt to identify spaces and
delimiters.
David.
>
> Best,
> Ista
>
>>
>>
>> On Sat, Aug 11, 2012 at 10:46 PM, Ista Zahn <istazahn at gmail.com>
>> wrote:
>>>
>>> Hi Sachin,
>>>
>>> There are at least two ways. The safer way is to use a regular
>>> expression to find the matching columns, like this:
>>>
>>> a <- initial_data[grep("^OFB[0-9]+", names(initial_data))]
>>>
>>> Alternatively, if you know that the columns you want are the first 8
>>> you can select them by position, like this:
>>>
>>> a <- initial_data[1:8]
>>>
>>> Best,
>>> Ista
>>>
>>> On Sat, Aug 11, 2012 at 7:59 AM, Sachinthaka Abeywardana
>>> <sachin.abeywardana at gmail.com> wrote:
>>>> Hi all,
>>>>
>>>> I have a data frame that has the columns OFB1, OFB2, OFB3,...
>>>> OFB10.
>>>>
>>>> How do I select the first 8 columns efficiently without typing
>>>> each and
>>>> every one of them. i.e. I want something like:
>>>>
>>>> a<-data.frame(initial_data$OFB1-10) #i know this is wrong, what
>>>> would be
>>>> the correct syntax?
>>>>
>>>> Thanks,
>>>> Sachin
David Winsemius, MD
Alameda, CA, USA