根据 R 中不同列的值的条件进行计算的函数

Function for calculating based on condition from value from different columns in R

我需要帮助来弄清楚如何在 R 中执行此操作。

我有一个 df 看起来像:

ID ZIP code loc 1 zip code loc 2 zip code loc 3 zip code Opinion on loc 1 Opinion on loc 2 Opinion on loc 3
1 90804 91723 90814 91604 Neutral Like Dislike

我正在尝试添加如下所示的三列:

ID ZIP code loc 1 zip code loc 2 zip code loc 3 zip code Opinion on loc 1 Opinion on loc 2 Opinion on loc 3 Distance from Like Distance from Disike Distance from Neutral
1 90804 91723 90814 91604 Neutral Like Dislike 42 Km 56 Km 24 Km

需要创建的列需要先匹配客户'Like'在哪个位置,计算他们与它的距离,然后将距离放入'Distance from Like'列等等。

我正在使用这个函数来计算距离:

library(zipcodeR)
zip_distance(zipcode_a, zipcode_b, lonlat = TRUE, units = "meters")

(来源:Conditional column value calculation, zip code to lat/long 上的第一个答案)

如果您更改您的列名,使它们的格式与 location_variable 一致,那么您可以使用 tidyr 的 pivot_long 将所有内容变成长格式,然后您可以计算所有距离作为所有位置的邮政编码现在都在一列中。然后,您可以将其保留为长数据格式,也可以将其恢复为您请求的宽格式

library(tidyverse)
test<- read.table( text = "ID,UserZIPcode,loc1_zipcode,loc2_zipcode,loc3_zipcode,loc1_Opinion,loc2_Opinion,loc3_Opinion
1,1,90804,91723,90814,91604,Neutral,Like,Dislike", sep = "," )

test_distances <- test %>% 
        pivot_longer(cols = c(-ID, -UserZIPcode), names_pattern  = "(.*)_(.*)", names_to = c("location",".value")) %>% 
        mutate(Distance = zip_distance(UserZIPcode, zipcode, lonlat = TRUE, units = "meters"))
  


test_distances %>% 
  pivot_wider(id_cols = ID, names_from = Opinion, names_prefix = "Distance_from", values_from = "Distance") %>% 
  right_join(test)