code missing values for many/all variables at once - spss

Suppose i have such table
if i use syntax to indicate missing values for variables, i must for each variable write something like
if missing (s1) s1=999.
MISSING VALUES s1 (999).
exe
if missing (s20) s20=999.
MISSING VALUES s20 (999).
exe
and so on.
but if i have 100 variables it will long and difficult.
is it possible to inditace missing values at once for all vars in my data
something like?
if missing (s1-q35) s1-q35=999.
MISSING VALUES s1-q35 (999).
exe

You can use recode like this:
recode s1 s2 s3 s4 s5 s6 .... q1 q2 q3 q4 q5 ..... (miss=999).
If some of your variables are consecutive in the data you can use "to". For example:
recode s1 to s21 q1 to q35 (miss=999).
If they are all consecutive you can use to for all of them:
missing values s1 to q35 (999).

Related

stack variables from netCDF file

I'm working with several netCDF files. Each nc file has 33 variables. I need to create a stack for each nc file with these 33 variables to do some calculations, but the only way I know is to convert each variable in a rater and stack them, one by one... Like this:
library(raster)
library(ncdf4)
library(rgdal)
nc_data <- nc_open('./data/GCAM/RAW/93d4aa096b15491b1ba136b46d8063cdca59d253c75d59791b4d4cb6f8a1ae91/Project ID 68344/GCAM-Demeter/GCAM-Harmonized/Mean_Std/GCAM_Demeter_LU_H_ssp1_rcp26_modelmean_2030.nc')
PTF0 <- nc_data$var[[1]]
data1 <- ncvar_get( nc_data, PTF0 )
data1 <- raster(data1)
plot(data1)
Can anyone help to automatize this?? I'm thankful in advance
This is a structure of the NetCDF file, I highlight the files that I need to stack, actually, I need stack just PT1 to PTF8
What you are doing now could be done like tis
f <- 'GCAM_Demeter_LU_H_ssp1_rcp26_modelmean_2030.nc'
library(raster)
r <- raster(f, "PTF0")
(assuming that "PTF0" is a variable name)
But if you want to create a single object for multiple variables in one step, use terra instead
library(terra)
r <- rast(f)
You can specify the variables you want
rr <- rast(f, c("PFT1", "PTF2"))
You can also create a SpatRasterDataSet and then extract the variables you want like this
s <-sds(f)
x <- rast(s[2:8])
Thanks Mr Robert! With these few lines, I solve the issue and I can do all I need.
library(raster)
library(ncdf4)
library(rgdal)
library(terra)
f <- nc_open('./data/GCAM/RAW/93d4aa096b15491b1ba136b46d8063cdca59d253c75d59791b4d4cb6f8a1ae91/Project ID 68344/GCAM-Demeter/GCAM-Harmonized/Mean_Std/GCAM_Demeter_LU_H_ssp1_rcp26_modelmean_2030.nc')
rr <- rast(f$filename)
rrr <- rr[[2:8]]
plot(rrr)
soma <- sum(rrr)
plot(soma)
Now I'll spend more time thinking about how to automate this... because I need to do this operation for each one of NetCDF files...

Modify values programmatically SPSS

I have a file with more than 250 variables and more than 100 cases. Some of these variables have an error in decimal dot (20445.12 should be 2.044512).
I want to modify programatically these data, I found a possible way in a Visual Basic editor provided by SPSS (I show you a screen shot below), but I have an absolute lack of knowledge.
How can I select a range of cells in this language?
How can I store the cell once modified its data?
--- EDITED NEW DATA ----
Thank you for your fast reply.
The problem now its the number of digits that number has. For example, error data could have the following format:
Case A) 43998 (five digits) ---> 4.3998 as correct value.
Case B) 4399 (four digits) ---> 4.3990 as correct value, but parsed as 0.4399 because 0 has been removed when file was created.
Is there any way, like:
IF (NUM < 10000) THEN NUM = NUM / 1000 ELSE NUM = NUM / 10000
Or something like IF (Number_of_digits(NUM)) THEN ...
Thank you.
there's no need for VB script, go this way:
open a syntax window, paste the following code:
do repeat vr=var1 var2 var3 var4.
compute vr=vr/10000.
end repeat.
save outfile="filepath\My corrected data.sav".
exe.
Replace var1 var2 var3 var4 with the names of the actual variables you need to change. For variables that are contiguous in the file you may use var1 to var4.
Replace vr=vr/10000 with whatever mathematical calculation you would like to use to correct the data.
Replace "filepath\My corrected data.sav" with your path and file name.
WARNING: this syntax will change the data in your file. You should make sure to create a backup of your original in addition to saving the corrected data to a new file.

How to order variables in Maxima?

I want to give variables a specific order in an equation in Maxima. This is display purposes only.
For example:
(%i1) E=(h*c)/%lambda;
c h
(%o1) E = -------
%lambda
I want the h and c variables to be in that order when displayed. I looked at ratvars() and ordergreat() but they don't appear to be relevant here.
Thanks for your help.
It appears that declare(<var>, mainvar) was what I was looking for. When mainvar attribute is declared for a variable it "succeeds all other constants and variables".
I was trying this using the STACK plugin for Moodle. I needed to remove the mainvar keyword from the forbidden list in the file casstring.class.php.
Actually, I think ordergreat() is the function you need, maybe you did a sorting before that needed unorder() first ro reset things.
Try
unorder()$ ordergreat (h, c)$ E=(h*c)/%lambda;
and
unorder()$ ordergreat (c, h)$ E=(h*c)/%lambda;

Mark all negative values of all variables as missing in SPSS

How can I mark all negative values of all variables as missing in SPSS? I have a dataset of more then 300 variables but none of the have missing values defined, but for all of them -1 and -2 should be treated as missing values. Is there a better way than to do it by hand for every variable?
If you really mean all values <= -1 and all variables, you can use a missing range, like this:
missing values all (lo thru -1).
Simplest way is
mis val v1 to v399 ((-2),(-1)).

BST printing without mutating?

So i basically want to printbst's .. here is a little more detail
Provide a function (printbst t) that prints a BST constructed from BST as provided by bst.rkt in the following format:
-Each node in the BST should be printed on a separate line;
-the left subtree should be printed after the root;
-The right subtree should be printed before the root;
-The key value should be indented by 2d spaces where d is its depth, or distance from the root. That is, the root should not be indented, the keys in its subtrees should be intended 2 spaces, the keys in their subtrees 4 spaces, and so on.
For example, the complete tree containing {1,2,3,4,5,6} would be printed like this:
6
5
4
3
2
1
Observe that if you rotate the output clockwise and connect each node to its subtrees, you arrive at the conventional graphical representation of the tree. Do not use mutation.
Here is what i have so far:
#lang racket
;;Note: struct-out exports all functions associated with the structure
(provide (struct-out BST))
(define-struct BST (key left right) #:transparent)
(define (depth key bst)
(cond
[(or (empty? bst) (= key (BST-key bst))) 0]
[else (+ 1 (depth key (BST-right bst)) (depth key (BST-left bst)))]))
(define (indent int)
(cond
[(= int 0) ""]
[else " " (indent (sub1 int))]))
(define (printbst t)
(cond
[(empty? t) (newline)]
[(and (empty? (BST-right t)) (empty? (BST-left t)))
(printf "~a~a" (indent (depth (BST-key t) t)) (BST-key t))]))
My printbst only prints a tree with one node thou .... i have an idea but it involves mutation, which i can't use :( ..... Any suggestions ? Should i change my approach to the problem all together?
Short answer: yes, you're going to want to restructure this more or less completely.
On the bright side, I like your indent function :)
The easiest way to write this problem involves making recursive calls on the subtrees. I hope I'm not giving away too much when I tell you that in order to print a subtree, there's one extra piece of information that you need.
...
Based on our discussion below, I'm going to first suggest that you develop the closely related recursive program that prints out the desired numbers with no indentation. So then the correct output would be:
6
5
4
3
2
1
Updating that program to the one that handles indentation is just a question of passing along a single extra piece of information.
P.S.: questions like this that produce output are almost impossible to write good test cases for, and consequently not great for homework. I hope for your sake that you have lots of other problems that don't involve output....

Resources