我的数据框中的变量包含字符观察值(不确定这是否是正确的方式来表示这一点,本质上,当我拉起结构时数据被列为“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') –