简体   繁体   English

R - 基于使用另一列的函数为一列添加值

[英]R - adding values for one column based on a function using another column

I have a dataset that looks like this我有一个看起来像这样的数据集

head(dataset)头部(数据集)

Distance   Lag time  Kurtosis
7.406100   10
144.1700   1
77.31800   1
81.15400   1
4.249167   6

I want to add values to the kurtosis column.我想向峰度列添加值。 To calculate kurtosis I need to group the Distances by Lag time (ie, all distances for Lag time 1 will give me one value for kurtosis etc.).要计算峰度,我需要按滞后时间对距离进行分组(即,滞后时间 1 的所有距离将为我提供一个峰度值等)。 To get kurtosis I usually use the package "psych" and function describe() Is there a kind of loop I could add to do this?为了获得峰度,我通常使用包“psych”和函数 describe() 有没有我可以添加的循环来做到这一点?

You should be able to do this using dplyr您应该可以使用dplyr执行此操作

library(dplyr)
library(magrittr)
dataset <- dataset %>%
           dplyr::group_by('Lag time') %>%
           dplyr::mutate(Kurtosis = describe(Distance)$kurtosis)

Since describe produces a dataframe as output and what you want is just one column (also named kurtosis) you'll need to subset the describe output由于describe生成一个数据帧作为输出,而您想要的只是一列(也称为 kurtosis),因此您需要对describe输出进行子集化

library(dplyr)
library(psych)

df %>% 
  group_by(Lag_Time) %>% 
  mutate(Kurtosis = describe(Distance)[1,"kurtosis"])

  Distance Lag_Time Kurtosis
     <dbl>    <dbl>    <dbl>
1     7.41       10    NA   
2   144.          1    -2.33
3    77.3         1    -2.33
4    81.2         1    -2.33
5     4.25        6    NA   

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

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