Reputation: 350
I have a dataframe that looks like this:
df <- data.frame(text = c("text1", "text2", "text3"),
a = c(1,2,3),
b = c(2,4,6),
c = c(3,6,9))
df
For each row I want to compute the mean of the distance between the values in columns a, b and c using:
mean(dist())
I want to store the outcome in a column named "score". The result should look like this:
df <- data.frame(text = c("text1", "text2", "text3"),
a = c(1,2,3),
b = c(2,4,6),
c = c(3,6,9),
score = c(mean(dist(c(1,2,3))),
mean(dist(c(2,4,6))),
mean(dist(c(3,6,9)))))
df
Searching Stackoverflow I could only find examples of converting one row to a vector. I also tried a bunch of my own approaches but each time I got stuck. This is probably due to a lack of base R knowledge. Please help me to solve this problem. I am very grateful for your help!
Upvotes: 1
Views: 3452
Reputation: 5017
Try this easy solution.
STEP 1: Create a function f doing the mean of all distance
f<-function(x)
{
return(mean(dist(x)))
}
STEP 2:
Apply the function by each row and insert the output in score
df$score<-apply(df[,-1],1,f)
Your output
df
text a b c score
1 text1 1 2 3 1.333333
2 text2 2 4 6 2.666667
3 text3 3 6 9 4.000000
Upvotes: 3
Reputation: 350
@JuanAntonioRoldánDíaz posted the correct answer.
df$score <- apply(df[,2:4], 1, function(x) mean(dist(x)))
Upvotes: 0
Reputation: 1658
I don't know if this will help you:
df <- data.frame(text = c("text1", "text2", "text3"),
a = c(1,2,3),
b = c(2,4,6),
c = c(3,6,9))
score= c(mean(dist(c(1,2,3))),
mean(dist(c(2,4,6))),
mean(dist(c(3,6,9))))
df = cbind(df,score)
The result is
df
text a b c score
1 text1 1 2 3 1.333333
2 text2 2 4 6 2.666667
3 text3 3 6 9 4.000000
A+
Upvotes: 0