- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the column number of a string column based on a string match in an R data frame?
A data frame might be very long and contain columns with only string values as well as numerical values. While doing the analysis, we might want to check which columns contain a particular string value. For example, if we have a column with string values as A, B, and C and we want to check which column contains a value “A” then apply function can be used as shown in the below examples.
Consider the below data frame −
x1<-sample(LETTERS[1:4],20,replace=TRUE) x2<-rpois(20,2) df1<-data.frame(x1,x2) df1
x1 x2 1 A 3 2 B 1 3 B 2 4 A 2 5 D 3 6 C 2 7 A 3 8 B 1 9 D 3 10 A 1 11 B 0 12 A 2 13 D 2 14 B 0 15 B 3 16 B 1 17 D 4 18 C 1 19 B 4 20 B 1
Checking the column where A exists -
sapply(df1, function(x) any(x=="A"))
x1 x2 TRUE FALSE
Here, the output for column x1 is TRUE, thus the column x1 contains A.
y1<-rnorm(20) y2<-sample(c("Rooh","Frank","Christina"),20,replace=TRUE) df2<-data.frame(y1,y2) df2
y1 y2 1 -1.05746268 Christina 2 -0.46353464 Rooh 3 -0.30686060 Rooh 4 0.22968835 Christina 5 0.36001582 Rooh 6 0.21773469 Christina 7 0.52068600 Christina 8 -0.28930238 Christina 9 2.75670872 Rooh 10 -0.70136439 Frank 11 1.49660551 Frank 12 0.02635582 Rooh 13 -2.20396449 Frank 14 0.82857546 Frank 15 0.36148329 Rooh 16 -0.82422499 Frank 17 -0.50081026 Rooh 18 -0.07842975 Christina 19 -1.19349053 Rooh 20 -0.55590686 Frank
Checking the column where Rooh exists -
sapply(df2, function(x) any(x=="Rooh"))
y1 y2 FALSE TRUE
- How to find the frequency of a particular string in a column based on another column in an R data frame using dplyr package?
- How to delete rows of an R data frame based on string match?
- How to extract columns based on particular column values of an R data frame that match a pattern?
- How to find the column means of a column based on another column values that represent factor in an R data frame?
- How to assign a column value in a data frame based on another column in another R data frame?
- How to create a new column in an R data frame based on some condition of another column?
- How to find the position of a data frame’s column value based on a column value of another data frame in R?
- How to add a new column in an R data frame with count based on factor column?
- How to find the length of the largest string in an R data frame column?
- How to match a column in a data frame with a column in another data frame in R?
- How to extract a data frame’s column value based on a column value of another data frame in R?
- How to get row index based on a value of an R data frame column?
- How to divide row values of a numerical column based on categorical column values in an R data frame?
- How to subset an R data frame based on string match in two columns with OR condition?
- How to find the frequency of continuously repeated string values in an R data frame column?