similar to: building a data.frame from a matrix: preventing conversion to factors

Displaying 20 results from an estimated 50000 matches similar to: "building a data.frame from a matrix: preventing conversion to factors"

2007 Oct 18
2
How to avoid conversion to factors (data frame to zoo)
Hi all, I was trying to convert a data frame to a zoo object so I can use some time series functions like lag(). But it seems then everything became a factor, so I have to convert it back to numeric to run the correct regressions. Is there a way to avoid it? Here is an example: ############################# a <- data.frame(nn =as.character(c("a", "b", "c",
2007 Jul 31
1
aggregate.data.frame - prevent conversion to factors? show statistics for NA values of "by" variable?
I have a two question regarding the "aggregate.data.frame" method of the "aggregate" function. My situation: a. My "x" variable is a data.frame ("mydf") with two columns, both columns of type/format "numeric". b. My "by" variable is a data.frame("mybys") with two columns, both columns of type/format "character". c.
2011 Feb 28
2
converting the string columns in a data.frame to factors?
Dear All, I'm not sure if I understand the parameter stringsAsFactors correctly. I'm trying to convert the string columns in aframe1 to factors. But it seems stringsAsFactors=T in as.data.frame() doesn't do anything. Could anybody let know what is the correct way to converting strings to factors? > aframe1=data.frame(x=LETTERS[1:10], y=LETTERS[1:10], stringsAsFactors=F) >
2011 Aug 22
1
Selecting cases from matrices stored in lists
Hi, I have two lists (c and h - see below) containing matrices with similar cases but different values. I want to split these matrices into multiple matrices based on the values in h. So, I did the following: years<-c(1997:1999) for (t in 1:length(years)) { year=as.character(years[t]) h[[year]]<-sapply(colnames(h[[year]]), function(var)
2012 Sep 16
2
multi-column factor
I have a data frame with columns which draw on the same underlying universe, so I want them to be factors with the same level set: --8<---------------cut here---------------start------------->8--- > z <- data.frame(a=c("a","b","c"),b=c("b","c","d"),stringsAsFactors=FALSE) > str(z) 'data.frame': 3 obs. of 2
2011 May 26
1
Divide matrix into multiple smaller matrices
Hi list, Using the script below, I have generated two lists (c and h) containing yearly matrices. Now I would like to divide the matrices in c into multiple matrices based on h. The number of matrices should be equal to: length(unique(DF1$B))*length(h). So each unique value in DF1$B get's a yearly matrix. Each matrix should contain all values from c where element cij is 1. An example for
2012 Apr 17
3
Can a matrix have 'list' as rows/columns?
After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. R-Inferno says... "Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the array—so two columns for a matrix. The result is a vector (not an array) containing the selected
2011 Aug 15
1
Selecting section of matrix
Hi, I have a question concerning the selection of data. Let's say that given list h created below, I would like to select a section of the 1999 matrix. For a case (rownames and colnames) I would like to select the cells that have a value > 0. So for case 8025 8025 8026 8027 8025 1 1 1 8026 1 1 1 8027 1 1 1 And for case 8028 8028 8029 8028 1
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
2017 Jul 08
0
Factor vs character in a data.frame vs vector
> On Jul 7, 2017, at 7:03 PM, John Kane <jrkrideau at yahoo.ca> wrote: > > Thanks Marc. > It never occurred to me that I would need a ""stringsAsFactors" expression in a data.frame. I could have sworn I never did before when mocking up some data but clearly I was wrong or there has been a change in R v. 3.4.1 which seems unlikely. Welcome John. Going back to
2004 May 14
5
Tagging identical rows of a matrix
I would like to generate a vector having the same length as the number of rows in a matrix. The vector should contain an integer indicating the "group" of the row, where identical matrix rows are in a group, and a unique row has a unique integer. Thus, for a <- c(1,2) b <- c(1,3) c <- c(1,2) d <- c(1,2) e <- c(1,3) f <- c(2,1) mat <- rbind(a,b,c,d,e,f) I would
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
2017 Jul 08
2
Factor vs character in a data.frame vs vector
Thanks Marc. It never occurred to me that I would need a ""stringsAsFactors" expression in a data.frame.? I could have sworn I never did before when mocking up some data but clearly I was wrong or there has been a change in R v. 3.4.1 which seems unlikely. On Friday, July 7, 2017, 10:37:29 AM EDT, Marc Schwartz <marc_schwartz at me.com> wrote: > On Jul 7, 2017, at 6:03
2012 Mar 14
2
Using the mantel test in Ape Package
Hi, I am trying to use a mantel test on two distance matrices. The code I have entered for each is: Gen_dists <- read.csv(file.choose(), stringsAsFactors = FALSE, na.strings = c(" "), fill = T,
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],
2005 Mar 10
1
R: LIST function and LOOPS
hi all another simple question. i've written a dummy program so that you get the concept. (the code could be simplfied such that there are no loops. but lets leave the loops in for now.) z1<-function(w) { for (i in 1:w) { set.seed(i+6) ss<-0 for (j in 1:5) { set.seed(j+1+(i-1)*6) r<-rnorm(1) ss<-ss+r } list(ss=ss) } } check.1<-z1(3) check.1 the results is: $ss [1]
2009 Mar 04
1
Eliminate Factors from Data Frame
Hi, I formed a 49 by 3 data frame by reading in a text file using read.table(), and combining it with a matrix that I made by using unlist() on a list of character strings. I would like to do some simple arithmetic operations on the elements in the data frame columns (e.g. column 3/column2) but the values in the data frame are stored as factors and using stringsAsFactors=FALSE did not work. I
2017 Jul 07
0
Factor vs character in a data.frame vs vector
> On Jul 7, 2017, at 6:03 AM, John Kane via R-help <r-help at r-project.org> wrote: > > This is not serious problem but I just wonder if someone can explain what is happening. > The same command within a dataframe is giving me a factor and as a plain vector is giving me a character. It's probably something simple that I have read and forgotten but I thought I'd ask.
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
2011 Apr 14
1
Create matrices for time series
Hi list, I would like to use the following data.frame to generate matrices over a 3 year moving window: DF = data.frame(read.table(textConnection(" A B C 80 8025 1995 80 8026 1995 80 8029 1995 81 8026 1996 82 8025 1997 82 8026 1997 83 8025 1997 83 8027 1997 84 8026 1999 84 8027 1999 85 8028 1995 85 8029 1998"),head=TRUE,stringsAsFactors=FALSE)) Function to be