Hello,

You should post a working example, we have no idea what your 2d data set is. A matrix? A data.frame? Something else?

And the string you are looking for? Are you thinking of regular expressions (grep) or is it a simple equality '=='?

Here is a reproducible example of the use of ?which() with argument arr.ind set to TRUE.

# create a data set
set.seed(2021)
A <- matrix(sample(letters, 24, TRUE), ncol = 4)

# Test for equality, this returns
# a logical matrix and which() can
# be applied to it
found <- A == "g"
which(found, arr.ind = TRUE)
#     row col
#[1,]   1   1
#[2,]   5   1
#[3,]   2   3


# The same code can be use if the data is
# a data.frame
df1 <- as.data.frame(A)
df1 == "g"


But if you want to look for a regex, try sapply. In this example the pattern is a simple one, and I use grepl.


pattern <- "g"
found2 <- sapply(df1, function(x) grepl(pattern, x))
which(found2, arr.ind = TRUE)


Hope this helps,

Rui Barradas



Às 18:07 de 15/05/21, Tuhin Chakraborty escreveu:
Hi,
How can I find the location of string data in my 2D dataset? spec(Dataset)
will reveal the columns that contain the strings. But can I know where
exactly the string values are in the column?

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to