我有兩個向量。我想對這兩個向量的每個可能的組合應用一個函數。爲了說明,假設我有以下兩個向量。r將函數應用於兩個列表
pets <- c('dog','cat','lemur')
fruit <- c('banana','apple','papaya')
我想將函數應用於以下變量組合,並將每個函數調用的結果與變量值一起保存。
[dog, banana]
[dog, apple]
[dog, papaya]
[cat, banana]
[cat, apple] etc...
什麼是在r中實現這個最好的方法?感覺這應該是申請家庭的工作。
可能'expand.grid(pets,fruit)' –
我看到expand.grid創建了這兩個向量的每個組合,但我希望能夠在每個組合上運行一個函數 – datavoredan
您可以嘗試像這樣'dplyr: :mutate(expand.grid(pets,fruit),function_output = paste(var1,var2))'其中'paste()'是一個示例函數 – Megatron