why cannot create rarecurve in vegan? - r

I am trying to create rarefaction curves for my data but my raremax and Srare are equal to zero and rarecurve does not create any curve. I have 36 obs and 52 variables, I am trying to assess any potential underestimation of species richness due to low sample size. I think that since the raremax is zero it will not create any curve. Thanks
This is my code:
library(vegan)
Moo<-read.csv("data.csv", strip.white=T)
#total number of species at each site (row of data)
S <- specnumber(Moo)
S
[1] 6 9 3 6 12 3 10 5 3 6 6 8 4 12 3 2 5 0 3 4 4 5 3 10 7 1 3 11 11
[30] 4 4 3 5 4 2 5
# Number of INDIVIDUALS per site
raremax <- min(rowSums(Moo))
raremax
[1] 0
Srare <- rarefy(Moo, raremax)
Srare
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
attr(,"Subsample")
[1] 0
#Plot rarefaction results
par(mfrow = c(1,2))
plot(S, Srare, xlab = "Observed No. of Species",
ylab = "Rarefied No. of Species",
main = " plot(rarefy(Moo, raremax))")
abline(0, 1)
rarecurve(Moo, step = 20,
sample = raremax,
col = "blue",
cex = 0.6,
main = "rarecurve()")
'> head(Moo)
Trapezia.aereolata Trapezia.globosa Trapezia.formosa Trapezia.lutea Trapezia.punctimanus
1 0 0 0 5 0
2 0 0 0 5 0
3 0 0 0 4 0
4 0 0 0 8 0
5 0 0 0 6 0
6 0 0 0 2 0
Trapezia.septata Trapezia.serenei Trapezia.tigrina Alpheus.lottini Alpheus.sp..White
1 0 0 0 4 0
2 0 0 0 4 0
3 0 0 0 0 0
4 0 0 0 2 0
5 0 0 0 4 0
6 0 0 0 1 0
Acanthanas.sp. Ophiuroids Ophiocoma.erinaceous Ophiocoma.sp Ophiactis.sp.
1 0 3 0 0 0
2 0 1 0 0 0
3 0 0 0 0 0
4 0 1 0 0 0
5 0 0 0 0 0
6 0 0 0 0 0
Echinometra.sp. Chlamys.sp. Conus.sp. Psaumis.cavipes Tiarina.sp. Calcinus.sp.
1 0 0 0 1 1 0
2 0 0 0 0 0 2
3 0 0 0 0 0 0
4 0 0 0 0 0 0
5 0 0 0 0 0 0
6 0 0 0 0 0 0
Harpiliopsis.sp. Jocaste.sp. Pilodius.pugil Pilodius.sp. Coralliocaris Chrysopetalum.Sp.
1 0 0 0 0 0 0
2 0 0 0 1 2 2
3 0 0 0 1 0 0
4 0 0 0 0 2 0
5 0 0 0 0 2 1
6 0 0 0 0 0 0
Menathius.sp. Chlorodiella.nigra Chlorodiella.sp. Synalpheus.sp. Labrid..wrasse.
1 0 0 0 0 0
2 1 1 0 0 0
3 0 0 0 2 0
4 0 0 0 0 1
5 0 0 0 0 0
6 0 0 0 0 0
Liomera.cinctimana Domecia.hispida Domecia Eviota.Sp. Paguris.sp. Palaemonidae Majiidae
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0
4 1 0 0 0 0 0 0
5 0 0 1 1 1 1 0
6 0 0 0 0 0 0 0
Cerithium.litteratum Cerithium.sp. Granulina..margaritula. Coralliophila.monodonta
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
5 1 3 0 0
6 0 0 0 0
Muricidae Tanaeid Unknown.shell. Amphipoda Polychaeta Strombus..marginatus.. Buccinidae
1 0 0 0 1 0 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0
5 0 1 1 0 0 0 0
6 0 0 0 1 0 0 0
gastropod..glasslike.shell Drupella
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0'

Related

R: Simulating ERGM model in R then generate adjacency matrix of that model

I use library(ergm) and library(igraph) and generate a ERGM network. But I want the adjacency matrix of that network. I am unable to find any function which can produce that.
library(ergm)
library(igraph)
g.use <- network(16,density=0.1,directed=FALSE)
#
# Starting from this network let's draw 3 realizations
# of a edges and 2-star network
#
g.sim <- simulate(~edges+kstar(2), nsim=3, coef=c(-1.8,0.03),
basis=g.use, control=control.simulate(
MCMC.burnin=1000,
MCMC.interval=100))
#g.sim[[3]]
summary(g.sim)
Is it possible to find the adjacency matrix from g.sim? and how?
EGRM package uses the network package and not the igraph package. You should maintain everythig in network and not load igraph as the two have some conflicting functions with same names.
In your case, you simulate 3 graphs thus you should have 3 adjacency matrices. The code is as below:
library(ergm)
g.use <- network(16,density=0.1,directed=FALSE)
g.sim <- simulate(~edges+kstar(2), nsim=3, coef=c(-1.8,0.03),
basis=g.use, control=control.simulate(
MCMC.burnin=1000,
MCMC.interval=100))
The code you want:
lapply(g.sim, as.matrix)
[[1]]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0
3 0 0 0 1 1 0 1 0 0 0 0 0 1 0 0 1
4 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0
5 1 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0
6 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0
7 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1
8 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 0
9 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
10 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0
11 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0
12 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
13 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0 1
14 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0
15 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
16 0 0 1 0 0 0 1 0 1 0 0 0 1 0 0 0
[[2]]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0
2 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
3 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0
4 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
6 0 0 0 0 0 0 0 1 1 0 1 1 1 0 0 1
7 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
8 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
9 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0
10 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
11 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0
12 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1
13 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0
14 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
15 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0
16 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0
[[3]]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
2 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0
3 0 0 0 0 1 0 0 0 0 1 1 0 0 0 1 0
4 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
5 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 1 0 0 0 0 1 0 1 0 0 0 1 0 1 0
7 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0
8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
10 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1
11 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1
12 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0
13 1 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0
14 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0
15 0 0 1 0 0 1 1 0 0 1 0 1 0 0 0 1
16 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0

how to make a blocked matrix?

suppose I have the following matrix
1 2 3
4 5 6
7 8 9
and I want to make a block matrix like:
1 2 3 0 0 0 0 0 0 0 0 0
4 5 6 0 0 0 0 0 0 0 0 0
7 8 9 0 0 0 0 0 0 0 0 0
0 0 0 1 2 3 0 0 0 0 0 0
0 0 0 4 5 6 0 0 0 0 0 0
0. 0 0 7 8 9 0 0 0 0 0 0
0 0 0 0 0 0 1 2 3 0 0 0
0 0 0 0 0 0 4 5 6 0 0 0
0 0 0 0 0 0 7 8 9 0 0 0
0 0 0 0 0 0 0 0 0 1 2 3
0 0 0 0 0 0 0 0 0 4 5 6
0 0 0 0 0 0 0. 0 0 7 8 9
I did following :
BigKernel<-Matrix::bdiag(replicate(4, m1, simplify = FALSE))
but the problem is that BigKernel is not a matrix. when I do
as.matrix(Bigkernel)
it is false. and types(Bigkernel) is S4.
How I can make a matrix?
R is case-sensitive. BigKernel and Bigkernel are different
as.matrix(BigKernel)
should work

How do I change color of interactions based on interaction value using an ifelse() statement in an plotweb bipartite?

Hi I am having trouble trying to get ifelse statements to work in a plotweb fuction (from bipartite) to color interaction based on the total quantity of interaction of each cell in the matrix. I had the same problem with the high bar colors, but since there were only a few values and one vector, it was easy to manually code.
Here is the code I am using, I want to color interactions greater than 15 as dark turquoise and keep the rest as default grey (grey80).
I have tried many different statements but I cant seem how to figure out what to put in the [,] to signify for the function to go through every individual cell and apply the statement instead of summing them, elem,elem also doesn't seem to work. Attached is a picture of the function's output currently
plotweb(LadyNet,
abuns.type='additional',
arrow="up.center",
text.rot=90,
col.low=c("olivedrab3"),
col.interaction =(ifelse(LadyNet[,] < 15,'grey80','darkturquoise')),
col.high = c("grey10","#FF0000","grey10","#FF0000","grey10","#FF0000","grey10","grey10","grey10"),
high.lab.dis = 0,
ybig=1.2,
y.width.high = .06,
high.spacing = 0.011,
y.lim = c(-1,2))
COCCAL COCSEP CYCPOL CYCSAN EXOFAS HIPCON PSYVIG SCY1 SCYMAR
Acmispon glaber 0 1 0 1 0 0 0 0 0
Ambrosia psilostachya 1 36 0 24 0 6 0 0 0
Artemisia douglasiana 0 0 0 1 0 1 0 0 0
Asclepias fascicularis 0 5 0 4 0 2 0 0 0
Avena fatua 6 10 0 0 0 4 0 0 0
Baccharis pilularis 9 76 0 38 0 27 0 1 0
Baccharis salicifolia 0 2 0 0 0 0 0 0 0
Bromus diandrus 1 8 0 0 0 4 0 0 0
Capsicum annuum 0 0 0 0 0 0 0 0 1
Chenopodium murale 0 1 0 0 0 0 0 0 0
Croton californicus 3 20 0 13 0 54 4 0 0
DEAD WOOD 0 1 0 0 0 0 0 0 0
Distichilis spicata 0 1 0 0 0 0 0 0 0
Echium candicans 0 1 0 3 0 0 0 0 0
Eleocharis acicularis 0 1 0 0 0 0 0 0 0
Encelia californica 1 1 0 3 0 2 0 0 0
Epilobium canum 0 0 0 1 0 0 0 0 0
Erigeron bonariensis 0 4 0 0 0 0 0 0 0
Erigeron canadensis 0 17 0 10 0 2 0 0 0
Erigeron sumatrensis 0 13 0 0 0 1 0 0 0
Eriophyllum confertiflorum 1 10 0 0 0 1 0 0 0
Fence 0 0 0 1 0 0 0 0 0
Festuca perennis 0 1 0 0 0 2 0 0 0
Gambelium speciosa 0 0 0 0 0 1 0 0 0
Geranium dissectum 0 0 0 3 0 0 0 0 0
GROUND 0 1 0 1 0 0 0 0 0
Helminthotheca echioides 0 1 2 17 0 1 0 0 0
Heterotheca grandiflora 2 92 0 12 0 7 1 0 0
Hirschfieldia incana 0 3 0 0 0 1 0 0 0
Juncus patens 0 1 0 0 0 0 0 0 0
Laennecia coulteri 1 65 0 2 0 3 0 0 0
Lobularia maritima 1 1 0 0 0 0 0 0 0
Morus sp. 0 0 0 1 0 0 0 0 0
NoPicture 4 3 0 3 3 2 3 0 0
Oxalis pes-caprae 4 6 0 0 0 2 0 0 0
Pennisetum clandestinum 1 5 0 0 0 0 0 0 0
Polygonum arenastrum 0 1 0 0 0 0 0 0 0
Raphanus sativus 0 1 0 0 0 0 0 0 0
ROCK 0 0 0 1 0 0 0 0 0
Rumex crispus 0 1 0 0 0 0 0 0 0
Rumex salicifolius 0 0 0 3 0 0 0 0 0
Salsola tragus 1 6 0 1 0 1 0 0 0
Salvia leucophylla 0 1 0 0 0 1 0 0 0
Schenoplectus americanus 0 1 0 0 0 0 0 0 0
Solanum nigrum 0 0 0 0 0 1 0 0 0
Sonchus arvensis 0 1 0 0 0 0 0 0 0
Spinacia oleracea 0 0 0 0 0 0 1 0 0
Stipa pulchra 0 1 0 0 0 0 0 0 0
Symphiotrichum subulatum 0 88 0 7 0 3 0 0 0
THATCH 1 3 0 0 0 4 0 0 0
Verbena lasiostachys 1 9 0 0 0 2 0 0 0
For Reference, I have gotten the ifelse statement to function properly in the plotweb function when there was only one species in the lower level attached is an example along with the code:
plotweb(rnet,
abuns.type='additional',
arrow="down.center",
text.rot=90,
col.low=c("olivedrab3"),
col.interaction =(ifelse(rnet[1,] < 12,'grey80','darkturquoise')),
col.high = (ifelse(rnet[1,] < 12,'grey10','darkturquoise')),
high.lab.dis = 0,
ybig=1.2,
y.width.high = .06,
high.spacing = 0.011)
One thing to note is that the col.interaction color matrix should be transposed.
Here is an example that I trust you will find useful:
library(bipartite)
library(grDevices)
plotweb(df,
abuns.type='additional',
arrow="up.center",
text.rot=90,
col.low=c("olivedrab3"),
col.interaction = t(ifelse(df[,] < 15,
adjustcolor('grey80', alpha.f = 0.5), #add alpha to colors
adjustcolor('darkturquoise', alpha.f = 0.5))),
col.high = c("grey10",
"#FF0000",
"grey10",
"#FF0000",
"grey10",
"#FF0000",
"grey10",
"grey10",
"grey10"),
bor.col.interaction = NA, #remove the black border color
high.lab.dis = 0,
ybig=1.2,
y.width.high = .06,
high.spacing = 0.011,
y.lim = c(-1,2))

how to convert a matrix of values into a binary matrix

I'd like to convert a matrix of values into a matrix of 'bits'.
I have been looking for solutions and found this, which seems to be part of a solution.
I'll try to explain what I am looking for.
I have a matrix like
> x<-matrix(1:20,5,4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
which I would like to convert into
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
2 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0
3 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
4 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0
5 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
so for each value in the row a "1" in the corresponding column.
If I use
> table(sequence(length(x)),t(x))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
5 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
9 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
13 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
14 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
17 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
18 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
this is close to what I am looking for, but returns a line for each value.
I would only need to consolidate all values from one row into one row.
Because a
> table(x)
x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
gives alls values of the whole table, so what do I need to do to get the values per row.
Here is another option using table() function:
table(row(x), x)
# x
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
# 2 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0
# 3 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
# 4 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0
# 5 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
bit_x = matrix(0, nrow = nrow(x), ncol = max(x))
for (i in 1:nrow(x)) {bit_x[i,x[i,]] = 1}
Let
(x <- matrix(c(1, 3), 2, 2))
[,1] [,2]
[1,] 1 1
[2,] 3 3
One approach would be
M <- matrix(0, nrow(x), max(x))
M[cbind(c(row(x)), c(x))] <- 1
M
# [,1] [,2] [,3]
# [1,] 1 0 0
# [2,] 0 0 1
In one line:
replace(matrix(0, nrow(x), max(x)), cbind(c(row(x)), c(x)), 1).
Following your approach, and similarly to #Psidom's suggestion:
table(rep(1:nrow(x), ncol(x)), x)
# x
# 1 3
# 1 2 0
# 2 0 2
We can use the reshape2 package.
library(reshape2)
# At first we make the matrix you provided
x <- matrix(1:20, 5, 4)
# then melt it based on first column
da <- melt(x, id.var = 1)
# then cast it
dat <- dcast(da, Var1 ~ value, fill = 0, fun.aggregate = length)
which gives us this
Var1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
2 2 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0
3 3 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
4 4 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0
5 5 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1

using lappy and elseif command

Using R I have a table, lets say 'locations'
head(locations, n=10)
apillar fender fwheel fdoor compart rdoor rwheel boot
1 0 0 0 0 0 0 0 1
2 0 0 0 1 0 0 0 0
3 0 0 0 0 1 0 0 0
4 0 1 0 0 0 0 0 0
5 1 0 1 0 0 0 0 0
6 1 0 0 1 0 0 0 0
7 0 0 0 0 0 0 0 0
8 0 0 0 0 1 0 0 0
9 0 0 0 1 0 0 0 0
10 0 0 0 0 0 1 0 0
now i want to create a new variable "cat" which groups the impacts into category locations.
I have been using if, elseif and else command, but I cannot get it to work.
The command is:
cat <- lapply(locations, function(x) if (apillar|fender|fwheel == 1)print("front") else if (fdoor|compart|rdoor == 1)print("middle") else if(rwheel|boot ==1)print("rear") else print("NA")
such that cat should read rear, middle, middle, middle, front etc
When vectors of TRUE or FALSE statements are involved, I usually prefer not to work with if to avoid loops. I find conditional referencing to be more elegant in this case. See below.
locations <- read.table(header=TRUE, text=
"apillar fender fwheel fdoor compart rdoor rwheel boot
1 0 0 0 0 0 0 0 1
2 0 0 0 1 0 0 0 0
3 0 0 0 0 1 0 0 0
4 0 1 0 0 0 0 0 0
5 1 0 1 0 0 0 0 0
6 1 0 0 1 0 0 0 0
7 0 0 0 0 0 0 0 0
8 0 0 0 0 1 0 0 0
9 0 0 0 1 0 0 0 0
10 0 0 0 0 0 1 0 0")
locations$cat <- NA
within(locations,{
cat[apillar|fender|fwheel] <- "front"
cat[fdoor|compart|rdoor] <- "middle"
cat[rwheel|boot] <- "rear"
})
Result:
apillar fender fwheel fdoor compart rdoor rwheel boot cat
1 0 0 0 0 0 0 0 1 rear
2 0 0 0 1 0 0 0 0 middle
3 0 0 0 0 1 0 0 0 middle
4 0 1 0 0 0 0 0 0 front
5 1 0 1 0 0 0 0 0 front
6 1 0 0 1 0 0 0 0 middle
7 0 0 0 0 0 0 0 0 <NA>
8 0 0 0 0 1 0 0 0 middle
9 0 0 0 1 0 0 0 0 middle
10 0 0 0 0 0 1 0 0 middle
Cheers!
Corrected your own code:
locations$cat= with(locations, ifelse(apillar|fender|fwheel, "front", ifelse(fdoor|compart|rdoor,"middle",ifelse(rwheel|boot, "rear", "NA"))) )
> locations
apillar fender fwheel fdoor compart rdoor rwheel boot cat
1 0 0 0 0 0 0 0 1 rear
2 0 0 0 1 0 0 0 0 middle
3 0 0 0 0 1 0 0 0 middle
4 0 1 0 0 0 0 0 0 front
5 1 0 1 0 0 0 0 0 front
6 1 0 0 1 0 0 0 0 front
7 0 0 0 0 0 0 0 0 NA
8 0 0 0 0 1 0 0 0 middle
9 0 0 0 1 0 0 0 0 middle
10 0 0 0 0 0 1 0 0 middle
>

Resources