[R] position of a string in a data frame

Duncan Murdoch murdoch.duncan at gmail.com
Wed Apr 30 14:12:37 CEST 2014


On 30/04/2014, 8:02 AM, Frede Aakmann Tøgersen wrote:
> Try
>
>   > foo <- data.frame(oid = 1:3, functions = c("mean", "sum", "sd"), type = c(TRUE, FALSE, TRUE))
>
>> foo
>    oid functions  type
> 1   1      mean  TRUE
> 2   2       sum FALSE
> 3   3        sd  TRUE
>
>> foo == "sum"
>         oid functions  type
> [1,] FALSE     FALSE FALSE
> [2,] FALSE      TRUE FALSE
> [3,] FALSE     FALSE FALSE
>
>> which(foo == "sum", arr.ind = TRUE)
>       row col
> [1,]   2   2
>>
>
> But can one risk this to fail if the class of some of the columns cannot be compared to a string?????

Yes, if some class declares that it's an error to compare it to a 
string, this could fail.

For example,

 > `==.foo` <- function(a, b) stop("don't do that")
 >
 > df <- data.frame(a=1)
 > class(df$a) <- "foo"
 > df == "abc"
Error in `==.foo`(left, right) : don't do that

The basic types allow comparison to strings.

Duncan Murdoch

>
> Yours sincerely / Med venlig hilsen
>
>
> Frede Aakmann Tøgersen
> Specialist, M.Sc., Ph.D.
> Plant Performance & Modeling
>
> Technology & Service Solutions
> T +45 9730 5135
> M +45 2547 6050
> frtog at vestas.com
> http://www.vestas.com
>
> Company reg. name: Vestas Wind Systems A/S
> This e-mail is subject to our e-mail disclaimer statement.
> Please refer to www.vestas.com/legal/notice
> If you have received this e-mail in error please contact the sender.
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On Behalf Of carol white
>> Sent: 30. april 2014 13:42
>> To: r-help at stat.math.ethz.ch
>> Subject: [R] position of a string in a data frame
>>
>> Hi,
>> It might be a primitive question but how to find the position of a string in a
>> data frame? Suppose I search the word "sum" (case insensitive) in a data
>> frame and it is in the 7th row, 3rd column, how to retrieve the indices 7, 3? I
>> tried to use grep with tapply but it doesn't work.
>>
>> Many thanks
>>
>> Carol
>>
>> 	[[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.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list