简体   繁体   中英

Calculate mean euclidean distance of multiple columns dataframe r

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!

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

@JuanAntonioRoldánDíaz发布了正确答案。

df$score <- apply(df[,2:4], 1, function(x) mean(dist(x))) 

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+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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