rdataframeparsingrowid

extract R data.frame rowid label to capture character rowid value from data.frame


R Select Conversion Problem

Head() command return results. I need extract R rowid numeric label to capture as character value from data.frame. Need to capture the rowid value plus column names. The Head() function returns the rowid as a label from the results. I need this label rowid on character format.

Currently the column names are "search" and "weight". When I attempt to use rowid_to_column(df[0]) on rowid, the outcome labels result naturally converts to numeric row numbers (this is correct expected behavior).

My data (see below in data.frame), is generated from glove_result() function. I only need a head() top list of 10 rows from the word vectors. With this result (return) from glove_results() function, I then parse the values as a data.framne with cbind() on attempt to add columns and rows. The result of this data.frame is see in below image.

R code: '''

glove_results <- function(word_vectors, gs) {
  cos_sim = sim2(x = word_vectors, y = gs, method = "cosine", norm = "l2")
  x <- head(sort(cos_sim[,1], decreasing = TRUE), 10)
  return(x)
}
g_findings <- glove_results(word_vectors, gs)
g_embbeded <- as.data.frame(cbind(w, cbind(word_terms=colnames(g_findings), weight=g_findings)))

'''

Data:

R data.frame

Challenge:

Need to capture rowid (seen here in data.frame image, with data.fram rowid's first column "mystery", "fellowship", etc.

Need to select this labeled rowid together with columns "search" and "weight" to make a new data.frame.

I have successfully selected / sliced column "search" and "weight" OK.The problem I have is with trying to capture the labeled rowid rows, e.g., "mystery", "fellowship" as character data, not as rowid numeric data.

My challenge is on how to capture rowid labels and extract R rowid label into character value from data.frame?


Solution

  • Found function to extract rowid as label / character.

    The function solution is: row.names.as.data.frame() or simple: row.names()

    I needed a data.frame for the rowid to be added as a column so this worked. But first needed to convert the matrix to a data.frame. Then apply row.names().

    For example:

    '''
    as.data.frame(word_terms=row.names.data.frame(df))
    '''