简体   繁体   English

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

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

this might seem like a really easy task, but I am a beginner in R. The task is as simple as the title explains it. 这似乎是一个非常容易的任务,但是我是R的初学者。任务很简单,正如标题所说明的那样。

I have a data frame and I would like to take all the text values from a row and put it into an array. 我有一个数据框,我想从一行中获取所有文本值并将其放入数组中。

I would like to use this statement, I just do not know what to use as a function: 我想使用此语句,我只是不知道将什么用作函数:

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

Is there a function to copy data or getText or is there an easier way ? 是否有复制数据或getText的功能,或者有更简单的方法吗?

My end goal is to take each row in the data frame and put it in an array, since I need to use an array for different of data analysis functions from a package. 我的最终目标是将数据框中的每一行放入一个数组中,因为我需要使用一个数组来处理包中不同的数据分析功能。

I know it is not good to write loops for this, so is there any function in the plyr package in R that would accomplish the end goal mentioned above ? 我知道为此编写循环不是很好,所以R中的plyr包中是否有任何功能可以实现上述最终目标?

Thanks for your time. 谢谢你的时间。

EDIT: code: 编辑:代码:

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")

I am trying to replicate this : https://sites.google.com/site/miningtwitter/questions/sentiment/sentiment @Tim Biegeleisen Look at the website. 我正在尝试复制此内容: https : //sites.google.com/site/miningtwitter/questions/sentiment/sentiment @Tim Biegeleisen查看网站。 Can you see a mistake ? 你能看到一个错误吗?

You can use the apply() function in row mode on your data frame. 您可以在数据框的行模式下使用apply()函数。 You can pass each row to the function classify_emotion like this: 您可以像这样将每一行传递给函数classify_emotion

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM