How to select columns by values ​​in a row in R

I have a large data frame indicating the appearance of trigrams in a row, where rows are strings, trigrams are columns, and values ​​indicate whether a trigram occurs in a row.

so something like this:

strs <- c('this', 'that', 'chat', 'chin')
thi <- c(1, 0, 0, 0)
tha <- c(0, 1, 0, 0)
hin <- c(0, 0, 0, 1)
hat <- c(0, 1, 1, 0)
df <- data.frame(strs, thi, tha, hin, hat)
df

#  strs thi tha hin hat
#1 this   1   0   0   0
#2 that   0   1   0   1
#3 chat   0   0   0   1
#4 chin   0   0   1   0

I want to get all columns / trigrams that have 1 for a given row or given row.

So, for line 2, the string is this, the result is a data frame that looks like this:

  str tha hat
1 this  0   0
2 that  1   1
3 chat  0   1
4 chin  0   0

How can i do this?

+4
source share
2 answers

This will give you the desired df result.

givenStr <- "that"
row <- df[df$strs==givenStr,]
df[,c(1,1+which(row[,-1]==1))]
+4
source

In one insert:

df[as.logical(df[df$strs=='that',])]

#  strs tha hat
#1 this   0   0
#2 that   1   1
#3 chat   0   1
#4 chin   0   0
+1
source

All Articles