Displaying 20 results from an estimated 50000 matches similar to: "Changing multiple instances in data.frame"
2018 Jan 08
2
Replace NAs in split lists
Thank you Jeff. Your code works, as usual , perfectly. I am just
wondering why if i put the whole code in one line, i get an error
message.
sdf2 <- lapply( sdf, function(z){z$Value
<-ifelse(is.na(z$Value),z$Value[!is.na(z$Value)][1],z$Value)z})
error. unexpected symbol in sdf2
Thanks again
EK
On Mon, Jan 8, 2018 at 3:12 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
>
2018 Jan 08
0
Replace NAs in split lists
Upon closer examination I see that you are not using the split version of
df1 as I usually would, so here is a reproducible example:
#----
df1 <- read.table( text=
"ID ID_2 Firist Value
1 a aa TRUE 2
2 a ab FALSE NA
3 a ac FALSE NA
4 b aa TRUE 5
5 b ab FALSE NA
", header=TRUE, as.is=TRUE )
sdf <- split( df1, df1$ID )
# note the extra [ 1 ]
2018 Jan 08
0
Replace NAs in split lists
I don't know. You seem to be posting in HTML so your code is mangled. Can you post plain text and use the reprex package to make sure it produces the errorin a clean R session?
--
Sent from my phone. Please excuse my brevity.
On January 8, 2018 8:03:45 AM PST, Ek Esawi <esawiek at gmail.com> wrote:
>Thank you Jeff. Your code works, as usual , perfectly. I am just
>wondering why
2018 Jan 08
2
Replace NAs in split lists
Hi
With the example, na.locf seems to be the easiest way.
> library(zoo)
> na.locf(df1)
ID ID_2 Firist Value
1 a aa TRUE 2
2 a ab FALSE 2
3 a ac FALSE 2
4 b aa TRUE 5
5 b ab FALSE 5
Cheers
Petr
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff
> Newmiller
> Sent: Monday, January
2018 Jan 08
3
Replace NAs in split lists
Why do you want to modify df1?
Why not just reassemble the parts as a new data frame and use that going forward in your calculations? That is generally the preferred approach in R so you can re-do your calculations easily if you find a mistake later.
--
Sent from my phone. Please excuse my brevity.
On January 7, 2018 7:35:59 PM PST, Ek Esawi <esawiek at gmail.com> wrote:
>I just came
2018 Jan 08
1
Replace NAs in split lists
OPS! Sorry i did indeed posted the code in HTML; should have known better.
ifelse(is.na(z$Value),z$Value[!is.na(z$Value)][1],z$Value)z})
error. unexpected symbol in sdf2
On Mon, Jan 8, 2018 at 11:44 AM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
> I don't know. You seem to be posting in HTML so your code is mangled. Can you post plain text and use the reprex package to
2018 Jan 08
0
Replace NAs in split lists
Yes, you are right if the IDs are always sequentially-adjacent and the first non-NA value appears in the first record for each ID.
--
Sent from my phone. Please excuse my brevity.
On January 8, 2018 2:29:40 AM PST, PIKAL Petr <petr.pikal at precheza.cz> wrote:
>Hi
>
>With the example, na.locf seems to be the easiest way.
>> library(zoo)
>
>> na.locf(df1)
> ID
2018 Jan 08
2
Replace NAs in split lists
You can enforce these assumptions by sorting on multiple columns, which
leads to
na.locf(df1[ order(df1$ID,df1$Value), ])
On Mon, Jan 8, 2018 at 4:19 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:
> Yes, you are right if the IDs are always sequentially-adjacent and the
> first non-NA value appears in the first record for each ID.
> --
> Sent from my phone. Please
2018 Jan 08
4
Replace NAs in split lists
Hi all--
I stumbled on this problem online. I did not like the solution given
there which was a long UDF. I thought why cannot split and l/s apply
work here. My aim is to split the data frame, use l/sapply, make
changes on the split lists and combine the split lists to new data
frame with the desired changes/output.
The data frame shown below has a column named ID which has 2 variables
a and b;
2018 Jan 08
0
Replace NAs in split lists
"Enforce" is overstating it... results will differ if there are no non-NA values for a given ID, and there is a potential further discrepancy if there are multiple non-NA values. But these issues were not identified by the OP, so may not be relevant in their case.
--
Sent from my phone. Please excuse my brevity.
On January 8, 2018 6:41:33 AM PST, Eric Berger <ericjberger at
2012 Jun 03
2
merging single column from different dataframe
Hi all,
probably really simple to solve, but having no background in programming I
haven't been able to figure this out: I have two dataframes like
df1 <- data.frame(names1=c('aa','ab', 'ac', 'ad'), var1=c(1,5,7,12))
df2 <- data.frame(names2=c('aa', 'ab', 'ac', 'ad', 'ae'),
var2=c(3,6,9,12,15))
Now I want merge
2018 Jan 08
0
Replace NAs in split lists
I just came up with a solution right after i posted the question, but
i figured there must be a better and shorter one.than my solution
sdf1[[1]][1,4]<-lapplyresults[[1]]
sdf1[[2]][1,4]<-lapplyresults[[2]]
EK
On Sun, Jan 7, 2018 at 10:13 PM, Ek Esawi <esawiek at gmail.com> wrote:
> Hi all--
>
> I stumbled on this problem online. I did not like the solution given
> there
2009 Sep 19
1
Re-order columns
Dear R'sians,
Would really appreciate if you could suggest a more efficient way to order
the columns of a dataset. The column names of the dataset contain indices
separated by a period. Following are examples of my code and the dataset.
oC <- function(tg=x2) {
lth <- length(grep("T",names(tg)))
thix <-
2007 Aug 24
3
Merging two files together in R
Hi,
Thanks in advance for reading this post.
I received some affymetrix genotyping data back recently (250K, Nsp
array)…However, in order for me to do any analysis on this data set, I need
to add append the annotation file to it. Basically I want to do something
that looks like this:
Snpfile(tab delimited):
SNPID Genotype X Y
123 AA 13.4 1.2
2011 Feb 09
3
Need help merging two dataframes
Hi R users,
I am trying to extract some attributes (age, sex, area) from dataframe "AB"
that has 101,269 observations of 28 variables to dataframe "t2" that has 47
observations of 6 variables. They share a column called "id", which is a
factor with 47 levels. I want to end up with a dataframe that has 47
observations of 9 variables (the original 6 variables of t2,
2011 May 12
2
Simple order() data frame question.
Clearly, I don't understand what order() is doing and as ususl the help for order seems to only confuse me more. For some reason I just don't follow the examples there. I must be missing something about the data frame sort there but what?
I originally wanted to reverse-order my data frame df1 (see below) by aa (a factor) but since this was not working I decided to simplify and order by
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.
2010 May 18
2
Counting Frequencies in Data Frame
Hi,
I am sure there is an easy way to do it, but I can't find it.
I have a data frame that has 15 columns and 7000 rows.
The only values inside the data.frame are "aa", "ab", "bb" as you can see an
example bellow.
1 2 3
1 aa ab ab
2 ab ab ab
3 aa aa aa
4 bb bb bb
What I would like to do, is to generate a vector (or another data.frame)
with 7000 rows, and 3
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
2016 May 13
2
Division entre el numero de ocurrencias parciales y totalesdentro de un DataFrame de manera eficiente
Hola:
Aplicaré lo que dices de usar data.table.
Sobre hacer for, etc. Ya lo he hecho, pero pensaba que usar dplyr haría la
tarea más rápida por estar este implementado en C (si no me equivoco).
Siempre que puedo utilizo estas funciones, porque codificas menos y van más
rápido. En este caso el problema es que no he encontrado la manera de hacer
lo que quiero con dplyr o similar. La idea era al