Displaying 20 results from an estimated 9000 matches similar to: "if else elseif for data frames"
2012 Aug 14
4
pass by reference
Hi all,
I want to do the following:
data<-data.frame(col1=c(1,2,3,4,5))
getcol2<-function(data){
data$col2[data$col1<=2]="L"
}
getcol2(data)
Unfortunately in the above col2 does not appear in the final data. So how
would you pass this by reference such that you would get it back?
Thanks,
Sachin
[[alternative HTML version deleted]]
2012 Aug 11
3
choosing multiple columns
Hi all,
I have a data frame that has the columns OFB1, OFB2, OFB3,... OFB10.
How do I select the first 8 columns efficiently without typing each and
every one of them. i.e. I want something like:
a<-data.frame(initial_data$OFB1-10) #i know this is wrong, what would be
the correct syntax?
Thanks,
Sachin
[[alternative HTML version deleted]]
2012 Aug 14
2
anova in unbalanced data
Hi all,
Say I have the following data:
a<-data.frame(col1=c(rep("a",5),rep("b",7)),col2=runif(12))
a_aov<-aov(a$col2~a$col1)
summary(aov)
Note that there are 5 observations for a and 7 for b, thus is
unbalanced. What would be the correct way of doing anova for this set?
Thanks,
Sachin
[[alternative HTML version deleted]]
2013 Jan 14
4
Grabbing Specific Words from Content (basic text mining)
Hi all,
Suppose I have a data frame with mixed content (name age and address).
a<-"Name: John Smith Age: 35 Address: 32, street, sub, something"
b<-data.frame(a)
1. The question is I want to extract the name age and
address separately from this data frame (containing potentially more
people).
2. Also just incase I have to deal with it how would the syntax change if I
had
2013 Jun 26
3
match rows of R
Hi all,
What would be an efficient way to match rows of a matrix to a vector?
ex:
m<-matrix(1:9, nrow=3)
m [,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
#################################
which(m==c(2,5,8)) # I want this to return 2
######################
Thanks,
Sachin
[[alternative HTML version deleted]]
2013 Sep 13
2
xtable use plus minus
I am using a similar dataset to the following:
a= c("Fruits", "Adam","errorA", "steve", "errorS",
"apples", 17.1,2.22, 3.2,1.1,
"oranges", 3.1,2.55, 18.1,3.2 )
a_table=data.matrix(t(matrix(a,nrow=5)))
I would like to plus minus every second column starting from errorA (using
xtable/ hmisc)
example output (ignoring
2013 May 02
3
Divide matrix columns by different numbers
Hi all,
I have a feeling the most efficient way to do the following is to use
apply, but I'm still wrapping my head around the function.
k=matrix(1:6,nrow=3)
div=1:2
Questions is how do I get R to divide the first column by 1 (div[1]) and
the second column by 2 (div[2])
k/div treats k as a vector and does the following (not what I want)
>k/div
[,1] [,2]
[1,] 1 2
[2,]
2013 Apr 18
3
Using different function (parameters) with apply
Hi All,
I have the following problem (read the commented bit below):
a<-matrix(1:9,nrow=3)
a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
div<-1:3
apply(a,2,function(x)x/div) ##want to divide each column by div-
instead each row is divided##
[,1] [,2] [,3]
[1,] 1 4.0 7
[2,] 1 2.5 4
[3,] 1 2.0 3
2012 Oct 18
4
speeding read.table
R 2.15.1
OS X
Colleagues,
I am reading a 1 GB file into R using read.table. The file consists of 100 tables, each of which is headed by two lines of characters.
The first of these lines is:
TABLE NO. 1
The second is a list of column headers.
For example:
TABLE NO. 1
COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10
2012 Aug 14
3
set working directory to current source directory
Hi all,
Is there a way to get cran R to set the working directory to be wherever
the source file is? Each time I work on a project on different computers I
keep having to set the working directory which is getting quite annoying.
Thanks,
Sachin
[[alternative HTML version deleted]]
2011 Nov 18
2
calling self written R functions
Hi All,
I have written a function (say) called foo, saved in a file called
foo.R. Just going by Matlab syntax I usually just change my folder path and
therefore can call it at will.
When it comes to R, how is the usual way of calling/loading it? because R
doesnt seem to automatically find the function from a folder (which might
be stupid to attempt in the first place).
Thanks,
Sachin
2012 Aug 16
8
How to extract from a column in a table?
Hi,
I have a table in which one column has the name of the objects as shown below.
Name
Budlamp-Woodcutter Complex - 15 to 60% slope (60/25/15)
Budlamp-Woodcutter Complex - 15 to 60% slope (60/25/15)
Terrarossa-Blacktail-Pyeatt Complex - 1 to 40% slope (40/35/15/10)
Terrarossa-Blacktail-Pyeatt Complex - 1 to 40% slope (40/35/15/10)
How can I split the single column into three columns
2011 Nov 28
2
efficient way to fill up matrix (and evaluate function)
Hi All,
I want to do something along the lines of:
for (i in 1:n){
for (j in 1:n){
A[i,j]<-myfunc(x[i], x[j])
}
}
The question is what would be the most efficient way of doing this. Would
using functions such as sapply be more efficient that using a for loop?
Note that n can be a few thousand. Thus atleast a 1000x1000 matrix.
Thanks,
Sachin
[[alternative HTML version
2012 Nov 01
2
Subsetting year range
Hi,
I have a panel data set that I am trying to subset. I am trying to keep
values for years >=1960.
The full set is from 1940 to 2000.
I tried a few things, but none worked. Here are a couple that I am trying to
use.
TableAPS1 <- subset(TableAP, year => 1959)
TableAPS1 <- TableAP[ which(year > 1959),]
It would be really nice if someone can point out what I am doing wrong
2011 Dec 06
2
configuring a package for own personal needs
Hi All,
There is a function in package "R2Cuba" called Cuhre that I need to use. It
keeps spitting out a new-line which I really dont want it to do. So I was
wondering what is the best way of configuring the package. I tried copying
and pasting the code into Cuhre2 and getting rid of the newline command BUT
that didn't work since it seems to have some private functions which I do
2013 Mar 08
2
Select rows from Data Frame with conditions
Hi all,
I have two dataframes. The first (A) contains all the stock prices for
today including today. So the first column is the stock Symbol and the
second column is the stock price. The second (B) is the symbol list in the
top 100 stocks.
I want to pick out from dataframe A only the rows containing the symbols
from B. i.e. something like:
prices <- A[A[,1]==B,2]
is there any
2010 Nov 10
5
arrays of arrays
Hi All,
I want to have an array/ matrix that looks this
x<- 0 0 1 1
1 3 5
4 4
7 -1 8 9 10 6
I hope this makes sense. So basically if I want x[1,3] it will access 0 and
similarly x[4,2], -1.
Thanks in advance,
Sachin
p.s. sorry about the corporate notice.
--- Please consider the environment before printing this email ---
Allianz - Best General Insurance Company of the Year 2010*
2012 Oct 12
3
Columns and rows
Hi,
Could you please advice some easy way to do the following for a dataframe
(header=F) having unequal column- & row- length.
1. Combine/stack/join contents from -
a) multiple rows into one column.
b) multiple columns into one row.
2. Stack contents from multiple columns (or, rows) into one column (or,
row).
Thank you.
Cheers,
Santana
[[alternative HTML
2011 Nov 18
2
assigning multiple outputs
Hi All,
So I figured out how to do multiple outputs, but whats the best/
recommended way of assigning them.
f<-function{a=1; b=1; list(a,b)}
I want to be able to say assign into a and b straight away rather that
doing a=f()[[1]] and b=f()[[2]]. It would be best if I can get around this
without having to assigning this to a third dummy variable.
Thanks,
Sachin
[[alternative HTML version
2006 May 19
2
my first R program
Hello,
This is my first attempt at using R. Still trying to figure out and understand
how to work with data frames.
I am trying to plot the following data(example). Some experimental data i am
trying to plot here.
1) i have 2 files
2) First File:
Number Position
1 120
2 134
3 156
4 169
5 203
3) Second File:
Col1 Col2 p-val
1 2 0.45
1 2 0.56
2 3 0.56
2 3 0.68
2 3