EFL
EFL

Reputation: 1008

R: Add new column to dataframe using function

I have a data frame df that has two columns, term and frequency. I also have a list of terms with given IDs stored in a vector called indices. To illustrate these two info, I have the following:

> head(indices)
   Term
1    hello
256  i
33   the

Also, for the data frame.

> head(df)
   Term  Freq
1  i     24
2  hello 12
3  the   28

I want to add a column in df called TermID which will just be the index of the term in the vector indices. I have tried using dplyr::mutate but to no avail. Here is my code below

library(dplyr)

whichindex <- function(term){
              ind <- which(indices == as.character(term))
              ind}

mutate(df, TermID = whichindex(Term))

What I am getting as output is a df that has a new column called TermID, but all the values for TermID are the same.

Can someone help me figure out what I am doing wrong? It would be nice as well if you can recommend a more efficient algorithm to do this in [R]. I have implemented this in Python and I have not encountered such issues.

Thanks in advance.

Upvotes: 2

Views: 40647

Answers (1)

npjc
npjc

Reputation: 4194

what about?

df %>% rowwise() %>% mutate(TermID = grep(Term,indices))

w/ example data:

library(dplyr)
indices <- c("hello","i","the")
df <- data_frame(Term = c("i","hello","the"), Freq = c(24,12,28))

df_res <- df %>% rowwise() %>% mutate(TermID = grep(Term,indices))
df_res

gives:

Source: local data frame [3 x 3]
Groups: <by row>

   Term Freq TermID
1     i   24      2
2 hello   12      1
3   the   28      3

Upvotes: 7

Related Questions