从数据框中的一行复制数据并将其放入 R 中的数组中

Copy data from a row inside a data frame and put it into an array in R

这似乎是一项非常容易的任务,但我是 R 的初学者。 正如标题所解释的那样,任务很简单。

我有一个数据框,我想从一行中取出所有文本值并将其放入一个数组中。

我想用这个语句,只是不知道用什么函数:

review = daply(.data = mydata[1,], .variables = mydata$names , .fun = ??? )

是否有复制数据或 getText 的功能,或者有更简单的方法吗?

我的最终目标是获取数据框中的每一行并将其放入数组中,因为我需要将数组用于包中的不同数据分析函数。

我知道为此编写循环不好,那么 R 中的 plyr 包中是否有任何函数可以实现上述最终目标?

感谢您的宝贵时间。

编辑: 代码:

result <- apply(mydata[1,], 1, function(x) { classify_emotion(x, algorithm="bayes", prior=1.0) })
 > emotion = result[,7]
 Error in result[, 7] : subscript out of bounds
 > emotion = result[,6]
 Error in result[, 6] : subscript out of bounds
 > emotion = result[7,] 
 > emotion[is.na(emotion)] = "unknown"
 > class_pol = apply(mydata[1,], 1, function(x) {classify_polarity(x, algorithm="bayes") })
 > polarity = class_pol[,4]
 Error in class_pol[, 4] : subscript out of bounds
 > polarity = class_pol[4,]
 > resu <- apply(mydata, 1, function(x) {data.frame(text=x, emotion=emotion,polarity=polarity, stringsAsFactors=FALSE) })
 > resu = within(resu ,emotion <- factor(emotion, levels=names(sort(table(emotion), decreasing=TRUE))))
 >  write.xlsx(resu, "C:/Users/Norbert/Desktop/resu.xlsx")

我正在尝试复制这个:https://sites.google.com/site/miningtwitter/questions/sentiment/sentiment @Tim Biegeleisen 查看网站。你能看出错误吗?

您可以在数据框的行模式下使用 apply() 函数。您可以像这样将每一行传递给函数 classify_emotion

result <- data.frame(apply(mydata, 1, function(x) {
                               y <- classify_emotion(x, "bayes", 1.0)
                               return(y)
                           }))