Suppose I have a string variable that takes on several string values:
gen rand = runiform()
sort rand
gen var1 = ""
replace var1 = "A" if rand < .3
replace var1 = "B" if rand>=.3 & rand < .7
replace var1 = "C" if var1==""
How would I change the values of var1? For example, A to be Aaa, B to be Bbb, and C to be Ccc?
I want to do something like the following (but rather replace the variables), which I know is incorrect:
label define var1L "A" "Aa" B "Bbb" C "Ccc"
label values var1 var1L
String variables can't have value labels. You can interpret this as if labels for string variables are meant to be the content of the variable itself. But you can replace:
clear
set more off
input ///
str1 var1
A
B
C
end
list
replace var1 = "Aaa" if var1 == "A"
replace var1 = "Bbb" if var1 == "B"
replace var1 = "Ccc" if var1 == "C"
list
You need to say more about your data and objectives for a more useful answer.
Related
I want to remove duplicate elements of string1 from string 2 and then output new string. My code works only if duplicate elements are in sequential order.
I want to work it any order of elements. Please advise.
Current Code:
set str1 "a 1 b 2 c 3 X Y Z"
set str2 "a 1 b 2 c 3 P Q R"
set results {}
set results [lmap a_elem $str1 b_elem $str2 {
if {$a_elem != $b_elem} {string cat $b_elem} else continue
}]
puts $results
Output of the following code :
P Q R
However, if
set str1 "a 1 b 2 c 3 X Y Z"
set str2 "P a 2 1 R c Q 3 b"
then Output will be : P a 2 1 R c Q 3 b
Basically same as str2 without the duplicate elimnation.
If you want to output those elements of the list in str2 that are nowhere in str1, you should first build a dictionary of the elements of str1 so that you can use efficient lookup (dicts are internally hash tables). You are strongly recommended to use a procedure for this as it makes the implementation rather more efficient.
proc removeItems {str1 str2} {
foreach item $str1 {
dict set items $item ""; # Value unimportant
}
lmap item $str2 {
if {[dict exists $items $item]} continue
string cat $item
}
}
puts [removeItems "a 1 b 2 c 3 X Y Z" "P a 2 1 R c Q 3 b"]
# P R Q
The code naturally assumes that the order of str2 is important.
If performance is not important, you can use the more straight forward:
set results [lmap elem $str2 {
if {$elem ni $str1} {string cat $elem} else continue
}]
I'm new to AMPL, and I would like to create a param C that map from a set A to set B:
file.mod:
set A;
set B;
param C{i in A} =
if i == "AA"
then
BA
else if i == "AB"
then
BB
else if i == "AC"
then
BC
else
BA;
data file.dat;
file.dat:
data;
set A := AA, AB, AC;
set B := BA, BB, BC;
When I try to compile this code, I get BA is not defined. If I replace the set element by string (BA becomes "BA"), then the error `Cannot convert character string to a number.``showed up.
Is there a way to achieve what I want to do?
Parameters in AMPL default to number. If you want to set a string parameter, you have to declare it as symbolic. (And yes, you need the quotes on those values.)
This seems to do what you want:
set A;
set B;
param C{i in A} in B symbolic =
if i == "AA"
then
"BA"
else if i == "AB"
then
"BB"
else if i == "AC"
then
"BC"
else
"BA";
data;
set A := AA, AB, AC;
set B := BA, BB, BC;
See section 7.8 of the AMPL Book for more information about symbolic parameters.
I have a plot with many objects and labels. So I wanted to simplify the srcipt using loops. But I don't know how to adress the variables. I define the variables as followed
V1 = 10
V2 = 20
V3 = 23
...
LABEL1 = a
LABEL2 = b
...
The loop should look something like that
set for [i=1:15] label i at V(i),y_label LABEL(i)
This notation leads to errors compiling the script. Is it possiple at all to define such a loop in gnuplot? If so how can I do it?
Thanks for your help!
You can define a function which formats the label-definition as string and use a do loop to evaluate the strings:
y_label = 0
V1 = 10
V2 = 20
V3 = 23
LABEL1 = "a"
LABEL2 = "b"
LABEL3 = "c"
do for [i=1:3] {
eval(sprintf('set label %d at V%d,y_label LABEL%d', i, i, i))
}
Alternatively, you can use two string with whitespace-separated words for the iteration:
V = "10 20 23"
LABEL = "a b c"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)
Note, that gnuplot 5.0 also has some limited support to use quote marks to hold several words together as one item:
V = "10 20 25"
LABEL = "'first label' 'second label' 'third one'"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)
I may be asking a silly question but I am self teaching myself VBA and I am just stumped and I am not even sure what terms I can use to look up a solution.
I am writing a code that will compare three variables to three other variables then I want to display which variables have changed.
So if x = a but y <> b and z <> c then the output should be b/c
I have worked out a code that works fine
Dim Str As String
If X <> A Then
If Y <> B Then
If Z <> C Then
Str = "a/b/c"
Else
Str = "a/b"
End If
ElseIf Z <> C Then
Str = "a/c"
Else
Str = "a"
End If
ElseIf Y <> B Then
If Z <> C Then
Str = "b/c"
Else
Str = "b"
End If
Else
Str = "c"
End If
But as I increase the number of variables this becomes extremely complex very quickly.
If anyone can help direct me to a simpler method without the exponential complexity I would be very grateful.
Thank you all so much!
You need to test each variable pair independently from each other -- not link them together in one giant If construct tree.
Example:
str = "" 'Start with blank string. Append as required.
If x <> a Then str = str & "a/"
If y <> b Then str = str & "b/"
If z <> c Then str = str & "c/"
'Remove the extra / at the end
If Right(str, 1) = "/" Then str = Left(str, Len(str - 1))
You could put the 2 strings in 2 arrays, and then use a FOR...NEXT construct to loop both arrays. You can use UBound(arValues) to dynamically find out the number of items in the array.
Good luck
Assigning empty string or string with letters results in GroovyCastException.Assigning a string with number values results in a number.What operation is happening here?
int var_1 = 2;
println var_1 // 2
var_1 = ""
println var_1 // GroovyCastException
int var_1 = 2;
println var_1 // 2
var_1 = "2"
println var_1 // 50
What operation/s results in 50?
It's considering "2" as a single character string, and assigning that character's Unicode value (U+0032 = '2') to the variable. So for example, I suspect if you do this:
var_1 = "A"
println var_1
you'll see 65 on the console
When you do something like this
var_1 = "2"
println var_1
Then the Unicode value corresponding to the character "2" got printed, which is 50.
Similarly if you will try to print the unicode value of "B" or "C" then you will get 66 or 67 as the results.
You can print a result 50 by doing this:
int var_1 = "2"
println var_1