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

152 views Asked by At

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.

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 ?

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 ?

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. Can you see a mistake ?

1

There are 1 answers

11
Tim Biegeleisen On BEST ANSWER

You can use the apply() function in row mode on your data frame. You can pass each row to the function classify_emotion like this:

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