R:使用函数将新列添加到数据框 [英] R: Add new column to dataframe using function
问题描述
我有一个数据框 df
,它有两列,术语和频率。我还有一个包含存储在一个向量索引
的向量中的给定ID的术语列表。为了说明这两个信息,我有以下内容:
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
另外,对于数据框。
> head(df)
Term Freq
1 i 24
2 hello 12
3 the 28
我想在 df
中添加一个名为 TermID
的列,这只是矢量索引
中的术语索引。我试过使用 dplyr :: mutate
,但无效。以下是我的代码
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))
我得到的输出是一个 df
,它有一个新的列 TermID
,但 TermID
的所有值都相同。
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.
有人可以帮我弄清楚我做错了什么吗?如果您可以在[R]中推荐更有效的算法来执行此操作,那么这也是很好的。我已经在Python中实现了这一点,我没有遇到这样的问题。
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.
提前感谢
推荐答案
如何?
df %>% rowwise() %>% mutate(TermID = grep(Term,indices))
w /示例数据:
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
:
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
这篇关于R:使用函数将新列添加到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!