0
我遇到了兩個寫數據框函數的問題。我經常得到一個有2個變量的數據框,我想將它們重新編碼爲一個變量。R:將2個變量重新編碼爲1內部函數
If V1>0 and V2 <0 then new_variable = "V1>0, V2<0.
在所有的數據框中,我有V1和V2有不同的名稱。
問題編號爲1 我不知道爲什麼test_df $ newVar,該功能後,只得到 「C> 0,I> 0」
#Using test FUN on example data frame
test_df.afterFUN <- test_fun(test_df, var1 = "V1", var2 = "V2", newVar = "category")
問題數2 爲什麼這最後一個參數函數「newVar」不會將名稱更改爲「類別」? 如果我運行此函數擬合到單個數據幀的代碼(重新命名變量和等。)它會工作,並給我什麼,我想(看test_df2)
rm(list = ls())
library("dplyr") # for filter
# Preparing example data frame
rama <- rbind(c(-5:20, -20:5), c(-20:5, -5:20))
rama <- t(rama)
colnames(rama) <- c("V1", "V2")
test_df <- as.data.frame(rama)
#Test FUN
test_fun <- function(df, var1, var2, newVar) {
df1 <- filter(df, var1 == 0, var2 == 0)
df1 <- mutate(df1, newVar = "C=0, I=0")
df2 <- filter(df, var1 == 0, var2 > 0)
df2 <- mutate(df2, newVar = "C=0, I>0")
df3 <- filter(df, var1 == 0, var2 < 0)
df3 <- mutate(df3, newVar = "C=0, I<0")
df4 <- filter(df, var1 > 0, var2 == 0)
df4 <- mutate(df4, newVar = "C>0, I=0")
df5 <- filter(df, var1 > 0, var2 > 0)
df5 <- mutate(df5, newVar = "C>0, I>0")
df6 <- filter(df, var1 > 0, var2 < 0)
df6 <- mutate(df6, newVar = "C>0, I<0")
df7 <- filter(df, var1 < 0, var2 == 0)
df7 <- mutate(df7, newVar = "C<0, I=0")
df8 <- filter(df, var1 < 0, var2 > 0)
df8 <- mutate(df8, newVar = "C<0, I>0")
df9 <- filter(df, var1 < 0, var2 < 0)
df9 <- mutate(df9, newVar = "C<0, I<0")
df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)
return(df)
}
#Using test FUN on example data frame
test_df.afterFUN <- test_fun(test_df, var1 = "V1", var2 = "V2", newVar = "category")
# Procedure outside of funcion fitted to test_df
df1 <- filter(test_df, V1 == 0, V2 == 0)
df1 <- mutate(df1, newVar = "C=0, I=0")
df2 <- filter(test_df, V1 == 0, V2 > 0)
df2 <- mutate(df2, newVar = "C=0, I>0")
df3 <- filter(test_df, V1 == 0, V2 < 0)
df3 <- mutate(df3, newVar = "C=0, I<0")
df4 <- filter(test_df, V1 > 0, V2 == 0)
df4 <- mutate(df4, newVar = "C>0, I=0")
df5 <- filter(test_df, V1 > 0, V2 > 0)
df5 <- mutate(df5, newVar = "C>0, I>0")
df6 <- filter(test_df, V1 > 0, V2 < 0)
df6 <- mutate(df6, newVar = "C>0, I<0")
df7 <- filter(test_df, V1 < 0, V2 == 0)
df7 <- mutate(df7, newVar = "C<0, I=0")
df8 <- filter(test_df, V1 < 0, V2 > 0)
df8 <- mutate(df8, newVar = "C<0, I>0")
df9 <- filter(test_df, V1 < 0, V2 < 0)
df9 <- mutate(df9, newVar = "C<0, I<0")
test_df2 <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)
謝謝你的回答。你知道如何把這個函數放在funtion中,test_fun(test_df,var1 =「V1」,var2 =「V2」,newVar =「category」),你在哪裏放置數據幀和變量的發言者? –
將代碼重寫爲函數;你可以調整其餘的。 – Laterow
非常感謝,現在我明白了這一點:)! –