Displaying 20 results from an estimated 50000 matches similar to: "How to fix my nested conditional IF ELSE code?"
2009 Jun 22
4
SAS-like method of recoding variables?
Dear R-helpers,
I am helping a SAS user run some analyses in R that she cannot do in
SAS and she is complaining about R's peculiar (to her!) way of
recoding variables. In particular, she is wondering if there is an R
package that allows this kind of SAS recoding:
IF TYPE='TRUCK' and count=12 THEN VEHICLES=TRUCK+((CAR+BIKE)/2.2);
Thanks for any help or suggestions you might be able
2023 Nov 03
2
I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
Just a minor point in the suggested solution:
df$LAP <- with(df, ifelse(G=='male', (WC-65)*TG, (WC-58)*TG))
since WC and TG are not conditional, would this be a slight improvement?
df$LAP <- with(df, TG*(WC - ifelse(G=='male', 65, 58)))
-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Jorgen Harmse via
R-help
Sent: Friday,
2009 Jun 08
5
if else
Hi R-helpers!
I have the following dataframe:
firm<-c(rep(1:3,4))
year<-c(rep(2001:2003,4))
X1<-rep(c(10,NA),6)
X2<-rep(c(5,NA,2),4)
data<-data.frame(firm, year,X1,X2)
data
So I want to obtain the same dataframe with a variable X3
that is:
X1, if X2=NA
X2, if X1=NA
X1+X2 if X1 and X2 are not NA
So my final data is
X3<-c(15,NA,12,5,10,2,15,NA,12,5,10,2)
2012 Feb 05
4
nested if else statements
I have a vector of 2,1,0 I want to change to 0,1,2 respectively (the data
is allele dosages)
I have tried multiple nested if/else statements and looked at the ?if help
and cannot work out what is wrong, other people have posted code which is
identical and they state works.
Any help would be greatly appreciated.
> A[1:20]
[1] 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 1 0 0
> B <-
2006 Feb 21
6
How to sum values across multiple variables using a wildcard?
I have a dataframe called "data" with 5 records (in rows) each of
which has been scored on each of many variables (in columns).
Five of the variables are named var1, var2, var3, var4, var5 using
headers. The other variables are named using other conventions.
I can create a new variable called var6 with the value 15 for each
record with this code:
> var6=var1+var2+var3+var4+var5
2011 Sep 03
2
problem in applying function in data subset (with a level) - using plyr or other alternative are also welcome
Dear R experts.
I might be missing something obvious. I have been trying to fix this problem
for some weeks. Please help.
#data
ped <- c(rep(1, 4), rep(2, 3), rep(3, 3))
y <- rnorm(10, 8, 2)
# variable set 1
M1a <- sample (c(1, 2,3), 10, replace= T)
M1b <- sample (c(1, 2,3), 10, replace= T)
M1aP1 <- sample (c(1, 2,3), 10, replace= T)
M1bP2 <- sample (c(1, 2,3), 10, replace= T)
2006 Feb 24
2
Sorting a dataframe by one column?
Given the following dataframe:
A=1:10
B=(a-5)^2
DATAFRAME=data.frame(A,B)
How can I sort DATAFRAME increasing (or decreasing, for that matter)
by B without making reference to A, or any other column?
I've read ?order and ?sort but cannot seem to figure this out.
Thank you.
Mark
2006 Feb 24
3
Sorting alphanumerically
I'm trying to sort a DATAFRAME by a column "ID" that contains
alphanumeric data. Specifically,"ID" contains integers all preceeded
by the character "g" as in:
g1, g6, g3, g19, g100, g2, g39
I am using the following code:
DATAFRAME=DATAFRAME[order(DATAFRAME1$ID),]
and was hoping it would sort the dataframe by ID in the following manner
g1, g2, g3, g6, g19,
2006 Apr 28
2
entries that match a regexp
Hi all,
I am trying to filter the element of a df that start with "TF", like
this:
alfa =
c(123221,"TF13124",41243,"TF1234",32414,"TF13124","TF14333",2134123,"TF1234")
beta =
2009 Jul 10
1
assessing data variation
I have data like so:
time datum
30 12
60 24
90 37
120 41
150 8
In addition to standard deviation, I want to measure the average of
the differences in data for each time interval, i.e. average of 24-12,
37-24, 41-37, 8-41. Is there a statistical term for this task? Which
package should I use please?
rhelp at conference.jabber.org
2008 Apr 29
2
Running regression (lm, lrm) 100+ times and saving the results as matrix
An undergraduate here, so do not hesitate to let me know if you feel that
I''m heading in a wrong direction.
I have a data frame containing panel data across 10 years (hence 120
months). I want to be able to run regression separately for each month (or
year). The below shows how I ran the regression for each month, but I need
to know how I would combine the regression results
2023 Nov 04
2
I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
I might have factored the gender.
I'm not sure it would in any way be quicker. But might be to some extent
easier to develop variations of. And is sort of what factors should be
doing...
# make dummy data
gender <- c("Male", "Female", "Male", "Female")
WC <- c(70,60,75,65)
TG <- c(0.9, 1.1, 1.2, 1.0)
myDf <- data.frame( gender, WC, TG )
#
2008 Aug 16
4
Dealing with NaN's in data frames
I am looking for the most efficient way to replace all occurrences of NaN in a data frame with NA. I can do this with a double loop, but it seems that there should be a higher level and more efficient way. With is.na, I could use ifelse, but if.nan seems not to have similar capabilities.
TIA,
Jon Peck
Jon K. Peck
jkpeck@aya.yale.edu
[[alternative HTML version deleted]]
2007 May 17
2
How to select specific rows from a data frame based on values
Dear Group:
I am working with a data frame containing 316 rows of individuals
with 79 variables. Each of these 79 variables have values that range
between -4 to +4, and I want to subset this data frame so that in the
resulting new dataframe, values of _all_ of these variables should
range between -3 and +3.
Let's say I have the following dataframe (it's a toy example with 4
individuals
2023 Nov 05
1
I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
There are many techniques Callum and yours is an interesting twist I had not considered.
Yes, you can specify what integer a factor uses to represent things but not what I meant. Of course your trick does not work for some other forms of data like real numbers in double format. There is a cost to converting a column to a factor that is recouped best if it speeds things up multiple times.
The
2008 Aug 22
2
Newbie programming help
All -
Not sure if this is a real programming question, but here goes:
I have data that looks like
Lake Length Weight
1 158 45
1 179 70
1 200 125
1 202 150
1 206 145
1 209 165
1 210 140
1 215 175
1 216 152
1 220 150
1 221 165
...
where lake goes from 1 - 84 and the number of rows for each lake is variable (but > ~20).
I'm trying to do two things: 1) build a simple linear model of the
2018 May 27
1
if else with 4 conditions problem
Hi everyone,
I have two columns:
? ?A? ? ? ? ? ? ? ?B
? ?1? ? ? ? ? ? ? ?1? ?1? ? ? ? ? ? ? ?0
? ?0? ? ? ? ? ? ? ?1
? ?0? ? ? ? ? ? ? ?0
I have 4 categories which are:
1) if A = 1 and B =1 then A1 = 1, else A2 = 0, A3 = 0, A4 = 0
2) if A = 1 and B =0 then A1 = 0, else A2 =1, A3 = 0, A4 = 0
3) if A = 0 and B = 1 then A1 = 0, else A2 = 0, A3 = 1, A4 = 0
4) if A = 0 and B =0 then A1 = 0, else A2 =
2006 May 05
2
boxplot - labelling
Hi,
How can I get the values of mean and median (not only points but values too) on the boxplot. I am using boxplot function from graphics package. Following is my data set
> df
[1] 5 1 1 0 0 10 38 47 2 5 0 28 5 8 81 21 12 9 1 12 2 4 22 3
> mean.val <- sapply(df,mean)
> boxplot(df,las = 1,col = "light blue")
> points(seq(df), mean.val,
2008 Feb 27
1
how to convert a table to adjacency matrix used in social network analysis?
Hi Guys,
Do you any one know how to convert a long format table to an adjacency
matrix used in sna? The long table looks like
p1 p2 counts
a b 100
a c 200
a d 100
b c 80
b d 90
b e 100
c d 100
c e 40
d e 60
and I want to convert it to an adjacency matrix which can be used in sna?
Any methods will be appreciated!
btw, besides sna package, is there any better package can be used in social
2007 Feb 09
3
How to count the number of NAs in each column of a df?
I would like to remove columns of a df which have too many NAs.
I think that summary() should give me the information, I just don't
know how to access it.
Advice?
_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS: P.O.Box 400400 Charlottesville, VA 22904-4400
Parcels: Room 102 Gilmer Hall
McCormick Road