R: Add new column to dataframe using function

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

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.

回答1:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!