[R] 'which' statement for recode?

2015-01-22 Thread Charles Stangor
Hi,

Is there a way to easily convert the list of course terms into sequential
integers in the dataframe (see code below)?

eg. 199801 = 1; 199808=2

I know I can use recode but shouldn't which work?

Thanks in advance!


sc = data.frame(c(200208, 200701, 201201))
names(sc) = c(TERM)

TermList = c(NA, 199801, 199808, 199901, 199908, 21,
28, 200101, 200108,
 200201, 200208, 200301, 200308, 200401, 200408,
200501, 200508,
 200601, 200608, 200701, 200708, 200801, 200808,
200901, 200908,
 201001, 201008, 201101, 201108, 201201, 201308,
201401, 201408)

which(TermList==sc$TERM)

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] which LETTERS?

2014-11-10 Thread Charles Stangor
I'm confused:

Thanks in advance.

 which(LETTERS == c(A))
[1] 1
 which(LETTERS == c(A,B))
[1] 1 2
 which(LETTERS == c(A,B,C))
[1] 1 2 3
Warning message:
In LETTERS == c(A, B, C) :
  longer object length is not a multiple of shorter object length


Charles Stangor
Professor
Dept of Psychology
University of Maryland
Academic Achievement Research Group http://www.charlesstangor.com/AARG

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Can't understand syntax

2012-07-15 Thread Charles Stangor
OK, I need help!!

I've been searching, but I don't understand the logic of some this
dataframe addressing syntax.

What is this type of code called?

test [[v3]] [is.na(test[[v2]])] -10  #choose column v3 where column v2
is == 4 and replace with 10

and where is it documented?


The code below works for what I want to do (find the non-missing value in a
row), but why?

test - read.table(text=
v1  v2  v3  result
3  NA  NA  NA
NA  3   NA NA
NA  NA   3 NA

, header=TRUE)

test [[result]] [!(is.na(test[[v1]]))] - test [[v1]] [!(is.na
(test[[v1]]))]
test [[result]] [!(is.na(test[[v2]]))] - test [[v2]] [!(is.na
(test[[v2]]))]
test [[result]] [!(is.na(test[[v3]]))] - test [[v3]] [!(is.na
(test[[v3]]))]

thanks!


On Fri, Jul 13, 2012 at 6:41 AM, Rui Barradas ruipbarra...@sapo.pt wrote:

 Hello,

 Check the structure of what you have, df and newdf. You will see that in
 df dateTime is of class POSIXlt and in newDf newDateTime is of class
 POSIXct.

 Solution:

 [...]
 df$dateTime - strptime(df$dateTime,%m/%d/%Y %H:%M)
 df$dateTime - as.POSIXct(df$dateTime)
 [...]

 Hope this helps,

 Rui Barradas

 Em 13-07-2012 10:24, vioravis escreveu:

 I have the following dataframe with the first column being of type
 datetime:

 dateTime - c(10/01/2005 0:00,
10/01/2005 0:20,
10/01/2005 0:40,
10/01/2005 1:00,
10/01/2005 1:20)
 var1 - c(1,2,3,4,5)
 var2 - c(10,20,30,40,50)
 df - data.frame(dateTime = dateTime, var1 = var1, var2 = var2)
 df$dateTime - strptime(df$dateTime,%m/%d/%Y %H:%M)

 I want to create 10 minute interval data as follows:

 minTime - min(df$dateTime)
 maxTime - max(df$dateTime)
 newTime - seq(minTime,maxTime,600)
 newDf - data.frame(newDateTime = newTime)
 newDf - merge(newDf,df,by.x = newDateTime,by.y = dateTime,all.x =
 TRUE)

 The objective here is to create a data frame with values from df for the
 datetime in df and NA for the missing ones. However, I am getting the
 following data frame with both Var1 and Var2 having all NAs.

  newDf

newDateTime var1 var2
 1 2005-10-01 00:00:00   NA   NA
 2 2005-10-01 00:10:00   NA   NA
 3 2005-10-01 00:20:00   NA   NA
 4 2005-10-01 00:30:00   NA   NA
 5 2005-10-01 00:40:00   NA   NA
 6 2005-10-01 00:50:00   NA   NA
 7 2005-10-01 01:00:00   NA   NA
 8 2005-10-01 01:10:00   NA   NA
 9 2005-10-01 01:20:00   NA   NA

 Can someone help me on how to do the merge based on the two datetime
 columns?

 Thank you.

 Ravi






 --
 View this message in context: http://r.789695.n4.nabble.com/**
 Merging-on-Datetime-Column-**tp4636417.htmlhttp://r.789695.n4.nabble.com/Merging-on-Datetime-Column-tp4636417.html
 Sent from the R help mailing list archive at Nabble.com.

 __**
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


 __**
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Charles Stangor
Professor and Associate Chair

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Substitute list value

2012-07-13 Thread Charles Stangor
thanks!

On Fri, Jul 13, 2012 at 4:35 AM, Jessica Streicher j.streic...@micromata.de
 wrote:

 two things:

 - R always counts from 1, not from 0
 - listmembers are accessed by using [[ ]] , not [ ]

 try

 t1[t==ll[[1]], v] - 99

 greetings Jessi


 On 11.07.2012, at 15:47, Charles Stangor wrote:

  I can't seem to determine how to get the name of a list member to
  substitute:
 
  ll - list(a1 = a,a2 = b)
 
  t1[t==ll[0], v] - 99
 
  why doesn't this substitute to:
 
  t1[t==a, v] - 99
 
  Thank you!
 
 
 
 
  --
  Charles Stangor
  Professor
 
[[alternative HTML version deleted]]
 
  __
  R-help@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.




-- 
Charles Stangor
Professor and Associate Chair

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] which() in subset()

2012-07-13 Thread Charles Stangor
Why does the subset not work in the which() version below?

Thank you


v1 - subset(t1,
 version_1==as.character(100-1)
 | version_1==as.character(100-2))

a-c(100-1, 100-2)
v1 - subset(t1, which(a==as.character(version_1)) != 0)

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] which() in subset()

2012-07-13 Thread Charles Stangor
Thanks Dennis and Mike... I'm getting it!!!

Sent from my Android

Rui Barradas ruipbarra...@sapo.pt wrote:

Hello,

To know why, just evaluate the condition, with 't1$' before 'version_1':

which(as.character(t1$version_1) %in% a) != 0
[1] TRUE TRUE


It allways evaluates to TRUE, therefore, subset() returns all rows.

See if this isn't simpler than both of your forms.

v2 - subset(t1, version_1 %in% a)
v2
   id version_1
1  1 100-1
2  2 100-2

The trick is to use %in% when doing multiple comparisons. With the 
vector with length equal to the number of observations on the left hand 
side.

Hope this helps,

Rui Barradas

Em 13-07-2012 12:12, Charles Stangor escreveu:
 Why does the subset not work in the which() version below?

 Thank you


 v1 - subset(t1,
   version_1==as.character(100-1)
   | version_1==as.character(100-2))

 a-c(100-1, 100-2)
 v1 - subset(t1, which(a==as.character(version_1)) != 0)

  [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Substitute list value

2012-07-11 Thread Charles Stangor
I can't seem to determine how to get the name of a list member to
substitute:

ll - list(a1 = a,a2 = b)

t1[t==ll[0], v] - 99

why doesn't this substitute to:

t1[t==a, v] - 99

Thank you!




-- 
Charles Stangor
Professor

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Substitute list value

2012-07-11 Thread Charles Stangor
I can't seem to determine how to get the name of a list member to
substitute:

ll - list(a1 = a,a2 = b)

t1[t==ll[0], v] - 99

why doesn't this substitute to:
t1[t==a, v] - 99

Thank you!

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Substitute list value

2012-07-11 Thread Charles Stangor
I can't seem to determine how to get the name of a list member to
substitute as a variable name:

ll - list(a1 = a,a2 = b)

t1[t==ll[1], v] - 99

why doesn't this substitute to:

t1[t==a, v] - 99

Thank you!




-- 
Charles Stangor
Professor



-- 
Charles Stangor
Professor

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with loop

2012-07-11 Thread Charles Stangor
I think I just learned this myself:

Don't put the $ extension in the bracket :

 df1$cola[is.na(df1$cola)]-

 df2$cola


Instead substitute using brackets within the brackets:
 df1[cola]is.na(df1[cola])]-

 df2[cola]



 then the cola s can be substituted.

Maybe this will help


On Wed, Jul 11, 2012 at 10:11 AM, paulalou pl...@medschl.cam.ac.uk wrote:

 Hi,

 I have two dataframes:

 The first, df1, contains some missing data:

cola colb colc cold cole
 1NA59   NA   17
 2NA6   NA   14   NA
 3 3NA   11   15   19
 4 48   12   NA   20

 The second, df2, contains the following:

   cola colb colc cold cole
 1  1.4  0.8 0.02  1.6  0.6

 I'm wanting all missing data in df1$cola to be replaced by the value of
 df2$cola. Then the missing data in df1$colb to be replaced with the
 corresponding value in df2$colb etc.

 I can get this to work column by column with single input lines but as my
 original dataset is a lot larger I'm wanting a create a loop but can't work
 out how.

 The single line command is:

  df1$cola[is.na(df1$cola)]-df2$cola

 I've tried a replace function within a loop but get error messages:

 list-colnames(df1)

 for (i in list) {
  r-replace(df1$i,df1$i[is.na(df1$i)],df2$i)
  }


 with error messages of:

 Warning messages:
 1: In is.na(mymat$snp) :
   is.na() applied to non-(list or vector) of type 'NULL'

 Can anyone help me with this?

 Thanks

 --
 View this message in context:
 http://r.789695.n4.nabble.com/Help-with-loop-tp4636140.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Charles Stangor
Professor and Associate Chair

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] sapply question

2012-07-11 Thread Charles Stangor
Why does this sapply code change df3 but not df1?

Thanks

df1 - read.table(text=
cola colb colc cold cole
1NA59   NA   17
2NA6   NA   14   NA
3 3NA   11   15   19
4 48   12   NA   20
, header=TRUE)

df2 -df1*2
df1
df2

df3 -sapply(names(df1),function(x) {df1[[x]]- df2[[x]]})
df1
df3

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Analyzing subsets

2012-07-11 Thread Charles Stangor
Say I want to perform transformations on row subsets of my dataframe.

Do I have to break the dataframe into subsets, perform the analysis on each
subset, and rbind() them together again?

..or is there another way.

Thank you!

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.