Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have two large data frames with same col names and same row names in the same order. Is there an R function to add element wise the two data frames together ?
Element-wise addition is what + does with most objects:
> d <- data.frame(x=1:3, y=4:6)
> d
x y
1 1 4
2 2 5
3 3 6
> d2 <- data.frame(z=4:6, w=6:4)
> d + d2
x y
1 5 10
2 7 10
3 9 10
The names will come from the first data frame, and order of the columns in the two sets does matter. As yours are in the same order, you should be fine.
You'll get an error if the number of rows or columns differ.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to create an integer vector in R with size n+1, but R cant understand that with "n" i mean all the natural numbers.
Something like this:
n = 1:10
my_vector <- n+1
my_vector
[1] 2 3 4 5 6 7 8 9 10 11
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to write a function that rearranges a vector in ascending or descending order. I know I can use sort and order functions but I want to do it manually.
If you want to practice writing your own sorting function, here is a example which applies a recursion approach:
mysort <- function(v, descending = F) {
if (length(v)==1) return(v)
if (descending) return(c(max(v),mysort(v[-which.max(v)],descending = descending)))
return(c(min(v),mysort(v[-which.min(v)])))
}
EXAMPLE
v <- c(1,2,5,4,2,7)
# ascending manner
mysort(v)
# descending manner
mysort(v,descending = T)
such that
> mysort(v)
[1] 1 2 2 4 5 7
> mysort(v,descending = T)
[1] 7 5 4 2 2 1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Prompted:
Given a vector of integers, write a function that returns a vector of those unique integers with multiple occurrences and put the result in a data frame.
I do not know how to isolate integers with multiple occurrences. Perhaps using the unique function?
I guess I would then want to display the results with something like:
table()
as.data.frame(table())
Any help would be much appreciated!
> sample(1:10, 10, replace=TRUE) -> x
> x
[1] 5 3 2 10 10 5 9 5 5 6
> y <- rle(sort(x))
> y$values[y$lengths > 1]
[1] 5 10
> y$lengths[y$lengths > 1]
[1] 4 2
Or using table:
> table(x)[table(x) > 1]
x
5 10
4 2
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have some file, and I want to merge one colume of them into a single vector, I use for-loop to read these file, but how to merge these colume. I just want to get the mean of these number.
As #akrun mentioned, it's hard to say without seeing some code or an example. That said, you might try append():
Suppose you have two vectors you'd like to conjoin:
> a <- c(1, 2, 3, 4, 5)
> b <- c(6, 7, 8, 9, 10)
You might use append() like so:
> c <- append(a, b)
> c
[1] 1 2 3 4 5 6 7 8 9 10
Then take the mean:
> mean(c)
[1] 5.5
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table like so:
1 2 3 4 5
a b a c
b b
a a a a
c b c b
Is there a special syntax to use for either Filter or Reduce (or something else entirely?) to get it so only the rows with an 'a' (including blanks) are shown? Likewise, is there a built-in way to count the frequency of 'a' for each column or would I have to loop over those individually?
Can't think of a way to pass rows or columns to Reduce or Filter to achieve the first, although a data.frame might get passed in a column-wise fashion for the second question since it is a list of columns. apply is the usual mechanism for doing row-wise operations, but I can think of quicker methods. For the first, under the assumption it is named X and is either a matrix or a data.frame:
X[ rowSums(X=="a", na.rm-TRUE) > 0 , ]
For the second:
colSums( X == "a")