I am working on a large dataset analysing survey data. From the data, I am producing cross-tabulations for different variables (c1, c2, c3). I am writing code whereby R will automatically pick out the “yes” value in the xtab, and give this a letter to use in subsequent analysis.
My problem starts where there are just yes or no values in the dataframe. When the cross tabulation is done, obviously, only the “no” or “yes” value is picked out.
Df1 <- data.frame(c = 1:4, c1 = c("yes","yes","yes","yes"), c2 = c("yes", "no", "no", "no"), c3 = c("no", "no", "no", "no"), weight = c(1.1, 1.2, 1.4, 0.8))
x<-xtabs(weight~c3,data=Df1)
y<-xtabs(weight~c2,data=Df1)
z<-xtabs(weight~c1,data=Df1)
When I try to assign a letter, the the output of the cross tabs, obviously it only works for the xtab that has both yes and no answers (b).
a<-x[2]
b<-y[2]
c<-z[2]
To get round this I tried using an “if” function, but it still is working yet. So, if there are yes answers in the xtab, this value should always be used, and just the no value is given, then a 0 should be assigned.
x1<-as.data.frame(x)
a<-if(x1$c3=="yes") x[2] else 0
y1<-as.data.frame(y)
b<-if(y1$c2=="yes") y[2] else 0
z1<-as.data.frame(z)
c<-if(z1$c1=="yes") z[2] else 0
I should get the answers a=0, b=1.1 and c=0, but so far, but limited r knowledge is not getting me very far indeed. any help would be much appreciated.
A
factora day keeps the doctor away. If you convert your data to factors, the R mechanism to keep track of categorical data, your task will be much easier.To convert a vector to a factor, use
factor. If you know in advance what the factor levels should be, specify that with thelevelsargument.You can apply this in a single statement to all of the necessary vectors with
lapply:Then your
xtabwill return the cross table with all the factor levels: