简体   繁体   English

在 R 中,如何在使用列值的每个 dataframe 行上应用 function?

[英]In R, how to apply a function on each dataframe row that uses a column value?

Let's say I have a dataframe假设我有一个 dataframe

Author |作者 | Lyrics |歌词 |

Name1 Text (characters) Name1 文本(字符)

Name2 Text (characters) Name2 文本(字符)

I want to create another column through applying a function that for each row takes the Text from the Text column, separates by whitespaces, then iterates over each token to see if it is within another vector I made (so I can work out the percentage of tokens within the text that are within that other vector).我想通过应用 function 来创建另一列,该列对于每一行从 Text 列中获取 Text,用空格分隔,然后遍历每个标记以查看它是否在我制作的另一个向量中(这样我就可以计算出文本中位于该其他向量中的标记)。

The function as I have written so far is below到目前为止我写的 function 如下

ReturnPercentPosWord = function(textLyrics){

WhitespaceSplitText = strsplit(textLyrics, " ")

LengthSplitText = length(WhitespaceSplitText)

CountInPosList = 0

for (i in WhitespaceSplitText) {

if (i %in% PositiveWords$word) {
  CountInPosList = CountInPosList+1
}

}

 if (CountInPosList == 0) {
return(0)

}

PercentInPos = (CountInPosList/LengthSplitText)*100

return(PercentInPos)}

I want to apply this function to each row now.我现在想将此 function 应用于每一行。 I have tried我努力了

TestPOSwordsDF$PercentPositiveWords = ReturnPercentPosWord(TestPOSwordsDF$Lyrics)

and

TestPOSwordsDF$PercentPositiveWords = apply(TestPOSwordsDF[, c('Lyrics'),drop=F], 1, ReturnPercentPosWord)

But I get a message saying the condition has length > 1 and only the first element will be used但是我收到一条消息,说the condition has length > 1 and only the first element will be used

I would really appreciate any help with this.我真的很感激这方面的任何帮助。 Thank you!谢谢!

Try using this:尝试使用这个:

TestPOSwordsDF$PercentPositiveWords <- sapply(
                   strsplit(TestPOSwordsDF$Lyrics, " "), function(x) 
                   mean(x %in% PositiveWords$word) * 100)

Here we split Lyrics on space, get the ratio of words which are present in PositiveWords$word .在这里,我们在空间上分割Lyrics ,得到PositiveWords$word中出现的单词的比率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 function 应用于 R 的 dataframe 中的每一列 - how to apply function to each column in dataframe of R R - 为矩阵的每个行/列应用具有不同参数值的函数 - R - Apply function with different argument value for each row/column of a matrix 将函数应用于R中数据框的列中的每个单元格 - apply a function to each cell in a column of a dataframe in R 如何计算依赖于使用每行变量值的函数的列? - How to compute a column that depends on a function that uses the value of a variable of each row? R将函数应用于数据框的每一行,将结果存储在同一数据框的新列中 - R apply function to each row of dataframe, store result in new column of same dataframe 如何在SPARKR DataFrame中的列的每个值上应用函数? - How do I apply a function on each value of a column in a SPARKR DataFrame? 使用 R 中的 apply 系列将两列 dataframe 的每一行传递给我制作的 function? - Using the apply family in R to pass each row of a 2-column dataframe to a function I made? 将 function 应用于 dataframe 中的列的每一行以创建新列 - Apply a function to each row of a column in a dataframe to create a new column 使用函数更改每行dataframe列的值 - Change value of each row of dataframe column with a function 将函数应用于R中数据框中每一行的特定列 - Apply a function to a specific column for every row in a dataframe in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM