Thank you Jeff for pointing out bad spreadsheet practices in R,
seconded by Mathew and Bert.
I should have considered creating a second dataframe ("test1_summary")
to distinguish raw from processed data. Those who want to address
memory issues caused by unnecessary duplication, feel free to chime
All:
1. I agree wholeheartedly with prior responses.
2. But let's suppose that for some reason, you *did* want to carry
around some "calculated values" with the data frame. Then one way to
do it is to add them as attributes to the data frame. This way they
cannot "pollute" the data in the way Jef
This does the summation you want in one line:
#create example data and column selection
d = as.data.frame(matrix(rnorm(50),ncol=5))
cols = c(1,3)
#sum selected columns and put results in new row
d[nrow(d)+1,cols] = colSums(d[,cols])
However, I would agree with the sentiments that this is a bad i
You can also look at the knitr-RMarkdown work flow, or the knitr-latex work
flow. In both of these it is reasonable to convert your data frame to a
temporary character-only form purely for output purposes. However, one can
usually use an existing function to push your results out without damagin
Again, you should always copy the R-help list on replies to your OP.
The short answer is you **shouldn't** replace NAs with blanks in your
matrix or dataframe. NA is the proper designation for those cell
positions. Replacing NA with a "blank" in a dataframe will convert
that column to a "characte
I'm sure there are more efficient ways, but this works:
> test1 <- matrix(runif(50), nrow=10, ncol=5)
> ## test1 <- as.data.frame(test1)
> test1 <- rbind(test1, NA)
> test1[11, c(1,3)] <- colSums(test1[1:10,c(1,3)])
> test1
HTH,
Bill.
William Michels, Ph.D.
On Fri, Mar 31, 2017 at 9:20 AM,
> dat <- data.frame(Group=LETTERS[1:5], X=1:5, Y=11:15)
> pos <- c(2,3)
> rbind(dat, Sum=lapply(seq_len(ncol(dat)), function(i) if (i %in% pos)
> sum(dat[,i]) else NA_real_))
Group X Y
1 A 1 11
2 B 2 12
3 C 3 13
4 D 4 14
5 E 5 15
Sum 15 65
> str(.Last.val
Let's keep r-list on the email per typical protocol. Apply is a function in
base R, so you don't need to install it
-Original Message-
From: Bruce Ratner PhD [mailto:b...@dmstat1.com]
Sent: Friday, March 31, 2017 1:06 PM
To: Doran, Harold
Subject: Re: [R] Taking the sum of
rg
Subject: Re: [R] Taking the sum of only some columns of a data frame
I do not believe this can be done in one step
dat <- data.frame(matrix(rnorm(50), 5))
pos <- c(1,3)
res <- apply(dat[, pos], 2, sum)
x <- numeric(5)
x[pos] <- res
rbind(dat,x)
-Original Message---
Ratner PhD
Sent: Friday, March 31, 2017 12:20 PM
To: r-help@r-project.org
Subject: [R] Taking the sum of only some columns of a data frame
Hi R'ers:
Given a data.frame of five columns and ten rows.
I would like to take the sum of, say, the first and third columns only.
For the remaining column
Hi R'ers:
Given a data.frame of five columns and ten rows.
I would like to take the sum of, say, the first and third columns only.
For the remaining columns, I do not want any calculations, thus rending their
"values" on the "total" row blank. The sum/total row is to be combined to the
original
11 matches
Mail list logo