Displaying 20 results from an estimated 4000 matches similar to: "select columns from vector of column names"
2010 Jul 09
1
select columns from dataframe
Hi,
I would like to extract columns from a dataframe using a vector of desired
column names.
The following working example uses the select argument in the subset
function to accomplish what I am trying to do. Is there a better solution?
Thanks.
#my data
data <- data.frame("col1"=c(1,2,3),"col2"=c("A","B","C"),"col3"=c(4,5,6))
2008 Sep 12
2
Finding a vector position using names rather than values
Dear R gurus,
I have been struggling with this for a while and thought you might be able to give me some guidance.
I have a data frame, on which I apply a row function. The result looks to me like a vector that retains the old row names. I then sort the vector and subsequently need to be able to identify the rank of certain values by searching for their row names. See this mock example:
>
2010 Jul 26
4
using string variable as order() function argument
Hello,
In my script I would like to use a loop, which sorts the dataframe
according to different columns, pointed by the string variable.
id col1 col2 col3
1 10 0 4 8
2 11 1 2 2
3 12 0 8 3
4 13 0 5 5
Usually the order() function can be used like this:
sorted = mytable**[order(column3) , ]
which results in properly sorted table:
**
2006 Jun 09
3
sqlSave() and rownames=TRUE makes my Rgui crash
Hello,
I created a table in MySQL with this command
CREATE TABLE example (pk INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(pk),
id VARCHAR(30),col1 VARCHAR(30),col2 VARCHAR(30))
### In R, I can connect to this table:
library(DBI)
library(RODBC)
chan <- odbcConnect("MySQL51", uid="root", pwd="xxx")
first <- sqlQuery(chan, "select * from example")
2011 Jul 08
1
Referencing a vector of data labels in ggplot function
Hi,
I really feel I've looked everywhere, although I know this can't be a hard
problem. I'd like to be able to call the graph below as a function, but I
can't get the function to recognize variables beyond 'dframe'. I've read
through many papers on writing functions in R, but I can't get this to work.
data <- data.frame('date' = as.Date(rep(c(15101,
2006 Mar 16
3
Did I use "step" function correctly? (Is R's step() function reliable?)
Hi all,
I put up an exhaustive model to use R's "step" function:
------------------------
mygam=gam(col1 ~ 1
+ col2 + col3 + col4
+ col2 ^ 2 + col3 ^ 2 + col4 ^ 2
+ col2 ^ 3 + col3 ^ 3 + col4 ^ 3
+ s(col2, 1) + s(col3, 1) + s(col4, 1)
+ s(col2, 2) + s(col3, 2) + s(col4, 2)
+ s(col2, 3) + s(col3, 3) + s(col4, 3)
+ s(col2, 4) + s(col3, 4) + s(col4, 4)
+ s(col2, 5) + s(col3,
2018 Feb 25
0
include
Hi Val,
My fault - I assumed that the NA would be first in the result produced
by "unique":
mydat <- read.table(textConnection("Col1 Col2 col3
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
val23<-unique(unlist(mydat[,c("Col2","col3")]))
napos<-which(is.na(val23))
preval<-data.frame(Col1=val23[-napos],
2018 Feb 25
2
include
HI Jim and all,
I want to put one more condition. Include col2 and col3 if they are not
in col1.
Here is the data
mydat <- read.table(textConnection("Col1 Col2 col3
K2 X1 NA
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
The desired out put would be
Col1 Col2 col3
1 X1 0 0
2 K1 0 0
3 Y1 0 0
4 W1 0 0
6 K2 X1
2018 Feb 25
3
include
Thank you Jim,
I read the data as you suggested but I could not find K1 in col1.
rbind(preval,mydat) Col1 Col2 col3
1 <NA> <NA> <NA>
2 X1 <NA> <NA>
3 Y1 <NA> <NA>
4 K2 <NA> <NA>
5 W1 <NA> <NA>
6 Z1 K1 K2
7 Z2 <NA> <NA>
8 Z3 X1 <NA>
9 Z4 Y1 W1
On Sat, Feb 24, 2018 at 6:18 PM, Jim
2018 Feb 25
2
include
Sorry , I hit the send key accidentally here is my complete message.
Thank you Jim and all, I got it.
I have one more question on the original question
What does this "[-1] " do?
preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
Col2=NA,col3=NA)
mydat <- read.table(textConnection("Col1 Col2 col3
Z1 K1 K2
Z2
2018 Feb 25
0
include
Jim has been exceedingly patient (and may well continue to be so), but this smells like "failure to launch". At what point will you start showing your (failed) attempts at solving your own problems so we can help you work on your specific weaknesses and become self-sufficient?
--
Sent from my phone. Please excuse my brevity.
On February 25, 2018 7:55:55 AM PST, Val <valkremk at
2018 Feb 25
0
include
hi Val,
Your problem seems to be that the data are read in as a factor. The
simplest way I can think of to get around this is:
mydat <- read.table(textConnection("Col1 Col2 col3
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
Col2=NA,col3=NA)
rbind(preval,mydat)
2009 May 15
1
creating and then executing command strings
Hi:
I very recently started experimenting with R and am occasionally
running into very basic problems that I can't seem to solve. If there
is an R-newbies forum that is more appropriate for these kinds of
questions, please direct me to it.
I'd like to automatically add vectors to a dataframe. I am able to
build command strings that would do what I want, but R is not
executing them.
A
2018 Feb 24
0
include
Thank you Jim and all, I got it.
I have one more question on the original question
What does this "[-1] " do?
preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
Col2=NA,col3=NA)
mydat <- read.table(textConnection("Col1 Col2 col3
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE)
2010 Oct 11
2
Split rows depending on time frame
Hi,
I have the following data frame, where col2 is a startdate and col3 an
enddate
COL1 COL2 COL3
A 40462 40482
B 40462 40478
The above timeframe of 3 weeks I would like to splits it in weeks like this
COL1 COL2 COL3 COL4
A 40462 40468 1
A 40469 40475 1
A 40476 40482 1
B
2013 Jan 11
3
split & rbind (cast) dataframe
Hi,
I would like to split dataframe based on one colum and want
to connect the two dataframes by rows (like rbind). Here a small example:
# The orgininal dataframe
df1 <- data.frame(col1 = c("A","A","B","B"),col2 = c(1:4), col3 = c(1:4))
# The datafame how it could look like
df2 <- data.frame(A.col2 = c(1,2), A.col3 = c(1,2), B.col2 = c(3,4),
B.col3
2010 May 11
1
create a data.frame for aov
Hi R-experts,
I try to find a way to transfer a matrix to a data.frame that is used as input of aov.
can you give me advice for that?
>mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE, dimnames = list(c("row1", "row2"), c("Col1", "Col2", "Col3")))
>mdat
Col1 Col2 Col3
row1 1 2 3
row2 11 12 13
===>
2018 Feb 24
3
include
On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
> x1 = rbind(unique(preval),mydat)
> x2 <- x1[is.na(x1)] <- 0
> x2 # gives 0
>
> Why introduce the 'x2'? x1[...] <- 0 alters x1 in place and I think that
> altered x1 is what you want.
>
> You asked why x2 was zero. The value of the expression
> f(a) <- b
> and assignments
2018 Feb 24
2
include
Hi All,
I am reading a file as follow,
mydat <- read.table(textConnection("Col1 Col2 col3
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE)
1. "NA" are missing should be replace by 0
2. value that are in COl2 and Col3 should be included in col1 before
they appear
in col2 and col3. So the output data looks like as follow,
X1 0 0
Y1 0 0
W1 0 0
Z2 0 0
Z3 X1 0
Z4 Y1
2010 Jul 24
2
union data in column
Is there any function/way to merge/unite the following data
GENEID col1 col2 col3 col4
G234064 1 0 0 0
G234064 1 0 0 0
G234064 1 0 0 0
G234064 0 1