我的數據框中的變量包含字符觀察值(不確定這是否是正確的方式來表示這一點,本質上,當我拉起結構時數據被列爲「chr」)。剔除少於兩個因子的變量
我想先把所有東西都轉換成因子,然後檢查一下因子水平。一旦它們成爲因素,我只想繼續使用具有兩個或更多級別的數據框中的變量。
這是我的想法。我知道for
循環在R中是一種禁忌,但我很新,對我來說使用它是有道理的。
x = as.character(c("Not Sampled", "Not Sampled", "Y", "N"))
y = as.character(c("Not Sampled", "Not Sampled", "Not Sampled", "Not Sampled"))
z = as.character(c("Y", "N", "Not Sampled", "Y"))
df = data.frame(x, y, z)
for i in df:
df$Response = as.factor(df[,i]) #create new variable in dataframe
df$Response = [email protected][sapply .... #where I think I can separate out the variables I want and the variables I don't want
m1 = lm(response ~ 1) #next part where I want only the selected variables
我知道解決方案可能要複雜得多,但這是我剛剛起步的嘗試。
哇,這是一個真棒小費,謝謝! – userfriendly
你不需要dplyr這個lapply方法。 (如果你想使用dplyr,你可以使用'mutate_each') –