recoding variable returns "undefined columns selected" - r

I have spent a couple hours trying to figure this out:
I need to reverse-code a variable. I have 3 identical variables at 3 differnet timepoints. The first 2 are easily recoded, but the third returns "undefined columns selected", even though I copy-paste the code. I also tried different versions of the same code, and have the same problem.
When I do print(dat$cuppass) it returns printed values, with 1 NA. I had similarly 1 or 2 NAs with the previous 2 timepoints, and have not had a problems when applying complete.cases.
Could anyone figure out how to recode 3d variable?
Thank you!!
these work:
dat$cdppassr <- 5-dat$cdppass[complete.cases(dat[dat$cdppass])]
print(dat$cdppassr)
dat$pdppassr <- 5-dat$pdppass[complete.cases(dat[dat$pdppass])]
print(dat$pdppassr)
Neither this,
dat$cuppassr <- 5-dat$cuppass[na.omit(dat[dat$cuppass])]
nor this works:
dat$cuppassr <- recode[na.omit(dat$cuppass,'0=5; 1=4; 2=3; 3=2; 4=1; 5=0')]
> print(cuppass)<br/>
[1] 5 5 5 5 4 3 4 3 3 3 5 4 5 3 5 5 3 3 5 3 3 3 3 4 3 3 4 2<br/>
[29] 3 3 5 2 3 2 5 3 3 4 5 3 4 5 3 5 4 5 5 4 3 5 3 5 3 4 3 5<br/>
[57] 4 5 4 4 4 3 3 5 4 4 5 5 3 4 4 4 3 3 3 5 5 5 3 5 3 4 3 3<br/>
[85] 2 4 3 3 4 5 2 5 5 3 4 4 3 5 5 5 3 5 5 5 3 5 5 3 5 3 5 5<br/>
[113] 4 5 5 3 3 5 3 2 5 5 5 5 4 5 3 5 4 5 5 5 5 4 5 5 3 5 5 5<br/>
[141] 3 5 5 5 3 3 2 5 4 2 3 5 4 5 5 3 3 5 5 5 3 3 5 5 5 NA 3 3<br/>
[169] 5 5 5 5 5 3 5 5 3 5 5 4 5 4 3 5 4 3 2 5 3 3 4 3 5 2 4 5<br/>
[197] 5 3 5 4 4 4 4 2 5 5 4 5 3 2 4 5 4 5 5 5 3 3 5 5 5 3 3 5<br/>
[225] 5 4 2 5 5 3 5 5 3 3 5 4 5 5 0 3 3 5 3 3 4 3 3 1 5 5 5 5<br/>
[253] 5 3 4 5 4 5 5 5 3 5 5 2 5 5 2 3 5 4 5 5 5 3 4 3 3 5 3 3<br/>
[281] 3 3 5 5 3 3 2 5 4 5 3 4 3 4 3 5 4 3 5 5 3 5 5 3 5 5 5 5<br/>
[309] 5 3 5 5 5 5 5 5 4 5 5 3 4 4 5 5 5 5 3 5 3 5 3 5 5 5 5 2<br/>
[337] 3 0 3 3 5 3 3 5 3 4 4 5 5 5 5 3 5 5 3 4 3 5 4 5 4 5 3 2<br/>
[365] 3 3 5 3 5 5 3 4 4 5 5 5 4 5 3 5 3 3 5 3 5 3 5 3 3 5 5 2<br/>
[393] 3 3 5 5 4 3 5 4 3 3 4 3<br/>

Related

How can I read an Elliptical Fourier Descriptor from SHAPE under the Momocs package of Rstudio?

I wish to load a CHC or NEF file from the SHAPE software in order to read the contours under MOMOCS and make the related analyses (Plot, PCA...)
I have difficulties under R as well as under the package.
How should I proceed?
Here is an extract of an outline. The first one is from a CHC and the other one from NEF.
Thank you in advance, any help is welcome.
CHC
IMG_20200710_0001_1 1045 531 1,428639E-02 46736 5 5 4 4 4 5 4 4 4 5 5 4 5 5 5 5 5 5 4 5 6 5 4 5 5 5 6 5 5 6 5 5 5 5 6 5 6 5 5 6 5 5 6 5 6 5 5 6 6 5 6 5 6 5 6 5 6 5 6 6 5 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 5 6 5 6 6 5 6 6 6 5 5 6 6 5 6 6 5 6 6 5 6 6 5 5 6 6 6 6 5 6 5 6 6 6 5 6 5 6 6 6 6 5 6 6 6 5 6 5 6 6 6 6 6 5 6 6 6 5 6 6 5 6 6 6 5 6 6 5 6 6 5 6 6 5 6 6 6 5 6 6 6 6 5 6 6 5 6 6 5 6 6 6 6 5 6 6 5 6 6 6 5 6 6 6 6 6 6 6 5 6 6 6 5 6 5 6 6 6 6 6 6 6 5 6 6 5 6 6 6 5 6 6 6 5 6 6 5 6 6 6 6 5 6 6 6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 1 2 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 1 2 2 1 2 2 1 2 2 2 1 2 1 1 2 2 2 2 1 2 2 1 2 2 1 2 2 2 1 2 2 1 2 2 2 1 2 2 1 2 2 2 1 2 2 1 2 2 2 1 2 2 2 1 2 2 2 2 1 0 2 2 2 2 1 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 3 1 0 2 1 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 3 1 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 1 2 2 3 2 2 2 2 3 2 2 2 2 2 2 2 2 2 3 2 2 2 2 3 2 2 2 3 2 2 3 2 2 3 2 2 3 2 3 2 3 2 2 3 3 2 4 3 2 3 3 4 4 -1
NEF
#CONST 1 1 1 0
#HARMO 15
IMG_20200710_0001_1
1.0000000e+00 3.5723303e-18 -2.2782437e-17 1.4062862e-01
-3.5345700e-03 -2.9677476e-04 -7.7529255e-02 2.8661995e-02
1.0149772e-01 3.3103762e-03 1.0055048e-03 3.3982778e-02
-5.6327330e-04 -3.2074328e-03 -5.6057424e-03 4.8512662e-03
3.8131276e-02 -1.5129959e-05 2.2552160e-03 1.4017338e-02
1.1789915e-04 -2.9600305e-03 -8.4295737e-04 -3.4299796e-04
1.7271413e-02 -7.2651006e-04 -1.2037379e-04 7.6870442e-03
-2.0549746e-04 -1.3433972e-03 -1.8426509e-03 -5.7843862e-04
1.0074513e-02 -9.3238318e-04 4.8671011e-05 5.6731905e-03
-7.5994187e-04 -1.0418345e-03 -7.7574542e-04 -2.8944747e-04
6.4933570e-03 -4.0572074e-04 -7.6458810e-04 4.0939891e-03
-5.4921053e-04 -8.4817676e-04 1.6206266e-04 -4.6108536e-04
4.5195469e-03 -5.4106090e-04 1.6321172e-04 3.1721989e-03
-1.8313986e-04 -3.5979483e-04 4.8276592e-04 -9.0690768e-04
3.0959844e-03 -6.1876139e-04 -6.3948596e-04 2.2105213e-03
In Momocs version 1.3.2 installed from GitHub https://github.com/MomX/Momocs/ I see the function chc2pix() that should import SHAPE's chain codes to R. In earlier Momocs documentation (https://www.rdocumentation.org/packages/Momocs/versions/0.2-03/topics/nef2Coe), you can find (in Examples!) the function nef2Coe(). I do not see this function in the current Momocs 1.3.2 but you can copy the whole function to your R script or save it to .txt and call it from R as source('path/to/your/NEF2COE.txt'). When the Momocs will be fully resurrected as MomX (https://momx.github.io/MomX/), perhaps the function again will be a part of the package.
sorry for the delay.
If you want to use CHC, then chc2pix is your friend indeed. You end up with an traditionnal outline as defined by its (x, y) coordinates.
For the NEF, it's "just" a simple string manipulation task. The clue pointed by #ordynets would likely work (or at least be very useful). Another option would be to go around :
# read the nef
m <- read.table("~/Desktop/nef.txt", skip=3)
# transpose to get in the "right" order
coe <- m %>% t %>% c %>% matrix(nrow=1)
# name columns to make thngs clear
colnames(coe) <- paste0(rep(LETTERS[1:4], times=nrow(m)),
rep(1:nrow(m), each=4))
# from now you can do whatever you want in Momocs
# eg rbind many to have an OutCoe, calculate the inverse EFT:
coe %>% coeff_rearrange("name") %>% coeff_split(nb.h=10) %>% efourier_i() %>% coo_plot()
(by the way, I'm still working on MomX, and such wrapper would fit in Momit but I struggle to find time to finish it).
do not hesitate to contact me directly !

spatstat deleting marks from a point pattern and subseuently the points

I am looking at the point pattern data set in spatstat anemones which has 231 points with marks attached to them which define the diameter.. I want to delete the marks and the points within the point pattern when the diameter is equal to 2
Here is the data:
>
[1] 6 4 4 6 3 3 5 3 5 4 4 6 5 3 4 7 4 6 6 5 4 4 5 3 3 6 4 5 4 4 5 3 3 5
3 4 5 8 5 4 6 5 6 4 5 3 3 4 5 6 4 4 3 4 4 6 5 4 3 6 5 3 [63] 3 6 5 3
3 2 5 7 4 4 4 3 3 4 3 6 2 6 6 3 4 3 7 6 3 4 2 7 4 5 4 4 4 6 4 3 3 3 3
6 7 3 7 3 2 4 3 5 2 3 4 4 3 3 3 6 3 4 5 3 6 3 [125] 7 5 3 3 4 4 5 4 4
6 5 3 3 3 5 3 6 5 5 4 4 3 4 4 4 4 3 4 7 4 6 5 7 6 3 6 5 4 6 4 5 4 5 3
6 3 3 6 4 6 4 4 6 3 5 3 4 6 5 5 4 5 [187] 4 3 3 4 4 4 4 5 4 5 5 5 4 6
4 4 5 3 5 4 3 4 4 4 3 4 5 5 3 3 5 3 4 5 6 2 5 2 3 2 3 3 7 5 4
thanks!
Another solution is to use the generic R command subset:
X <- subset(anemones, marks != 2)
From the question it is not quite clear whether you want to get rid of all the marks after deleting these points. In that case use unmark:
X <- unmark(X)
Correct me if I'm wrong but I'm reading this as meaning you wish to remove observations when anemones$marks is equal to 2.
If so this should do it:
updated_anemones <- anemones[!anemones$marks == 2,]

Subset an S4 object in R

I have an S4 object 'DATA' with three slots - the structure looks like this (sorry, I don't know how to create an example, so this a simplified structure of what I'm working with)
---Coordinates: #coo (e.g. 'Name_A') is a 2x20 matrix where, there are 6 matrices with names
---Names: #names Name_A Name_B Name_C Name_D Name_E Name_F and each Name has one of two Factors
---Factors: #fac Factor_A Factor_B
How do I subset (select) #coo data where #fac = Factor_A? I've tried a numer of combinations of dataframe type selections using [] notation with getElement but this is clearly not correct. Thanks.
EDIT
Repeatable example - this is a horrific way of doing this I know....
library(Momocs)
The input data file you read in looks like this (called "test.chc", where this is the output from another application)
[1] "sampleA 322 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[2] "sampleB 231 5 4 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[3] "sampleC 489 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[4] "sampleD 323 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[5] "sampleE 381 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[6] "sampleF 342 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[7] "sampleG 327 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[8] "sampleH 336 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[9] "sampleI 147 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
[10] "sampleJ 654 6 5 4 4 4 4 4 6 5 4 4 4 6 0 0 0 6 6 4 6 5 4 4 4 4 3 2 4 3 2 4 4 -1"
and
grps<- data.frame(Groups=(rep(letters[1:2],5)))
c.S4obj <-chc2Coo("test.chc")
c.S4obj#fac <-grps
str(c.S4obj)
So, the question is, how to select data for a specific group (a or b) and return an S4 class object?
Momocs now uses S3 classes and it's much easier to retrieve data. If you're still doing morphometrics, it's worth having a look to the 1.0.0 version on CRAN and its vignette.

using Reduce/do.call with ifelse

This is purely a curiosity (learning more about Reduce). There are way better methods to achieve what I'm doing and I am not interested in them.
Some people use a series of nested ifelse commands to recode/look up something. Maybe it looks like this:
set.seed(10); x <- sample(letters[1:10], 300, T)
ifelse(x=="a", 1,
ifelse(x=="b", 2,
ifelse(x=="c", 3,
ifelse(x=="d", 4, 5))))
Is there a way to use either do.call or Reduce with the ifelse to get the job done a little more eloquently?
Try this:
> library(gsubfn)
> strapply(x, ".", list(a = 1, b = 2, c = 3, d = 4, 5), simplify = TRUE)
[1] 5 4 5 5 1 3 3 3 5 5 5 5 2 5 4 5 1 3 4 5 5 5 5 4 5 5 5 3 5 4 5 1 2 5 5 5 5
[38] 5 5 5 3 3 1 5 3 2 1 5 2 5 4 5 3 5 2 5 5 5 4 5 1 2 5 4 5 5 5 5 1 3 1 5 5 5
[75] 1 5 4 5 3 3 5 5 3 5 3 1 5 3 2 2 5 5 5 5 4 5 3 5 5 1 4 1 4 5 5 5 5 5 5 5 5
[112] 5 2 5 5 5 3 5 5 5 2 4 4 5 3 3 5 4 5 5 5 1 5 3 4 3 5 5 2 5 5 3 1 5 2 5 5 5
[149] 1 5 5 2 1 2 4 2 2 3 5 2 5 5 5 5 5 3 5 5 5 5 5 5 5 5 5 5 2 3 5 4 4 2 5 5 5
[186] 5 5 5 5 2 1 1 1 5 5 5 5 3 5 5 3 5 5 5 2 5 5 5 3 5 5 5 5 5 1 5 5 5 5 2 2 5
[223] 5 5 4 3 4 5 5 4 5 5 5 3 5 3 5 5 5 5 4 5 5 1 5 5 2 5 5 5 2 5 5 3 2 5 4 5 2
[260] 5 5 3 5 5 1 4 3 5 4 5 2 5 5 3 5 5 5 5 5 1 1 5 2 5 1 5 5 5 5 5 5 5 5 5 5 5
[297] 5 1 5 2
Here is an attempt. It is neither beautiful nor does it use ifelse:
f <- function(w,s) {
if(is.null(s$old))
w$output[is.na(w$output)] <- s$new
else
w$output[w$input==s$old] <- s$new
return(w)
}
set.seed(10); x <- sample(letters[1:10], 300, T)
subst <- list(
list(old="a", new=1),
list(old="b", new=2),
list(old="c", new=3),
list(old="d", new=4),
list(old=NULL, new=5)
)
workplace <- list(
input=x,
output=rep(NA, length(x))
)
Reduce(f, subst, workplace)

What is the Matlab/Octave equivalent or R's 'merge' (or 'expand.grid')?

I am looking for the Matlab way of doing the following:
> merge(2:4,3:7)
x y
1 2 3
2 3 3
3 4 3
4 2 4
5 3 4
6 4 4
7 2 5
8 3 5
9 4 5
10 2 6
11 3 6
12 4 6
13 2 7
14 3 7
15 4 7
> expand.grid(2:4,3:7)
Var1 Var2
1 2 3
2 3 3
3 4 3
4 2 4
5 3 4
6 4 4
7 2 5
8 3 5
9 4 5
10 2 6
11 3 6
12 4 6
13 2 7
14 3 7
15 4 7
I usually do it with meshgrid:
>> [x y] = meshgrid(2:4, 3:7);
>> [x(:) y(:)]
ans =
2 3
2 4
2 5
2 6
2 7
3 3
3 4
3 5
3 6
3 7
4 3
4 4
4 5
4 6
4 7
Use ndgrid for n variables (2 and more). For example (4-D space)
[X,Y,Z,T] = ndgrid(2:4, 3:7, 1:2, 1:10);

Resources