Need help in using R "as." function [duplicate] - r

This question already has answers here:
issue summing columns
(2 answers)
Closed 6 years ago.
a <- x
where x is data frame with 120 columns in it.
sum(a$column1) == 0
this condition works fine.
I have a situation where I have to find each column’s sum.
So created vector m with list of all column names in it
m <- colnames(a)
tried calling the vector values inside the sum function it is throwing error.
sum(m[1]) == 0
throws some error. Not sure which as. Function to use here.

is sapply(x, sum) what you want?

Related

Subtract values from vector [duplicate]

This question already has answers here:
What does the diff() function in R do? [closed]
(2 answers)
Closed 5 months ago.
I dont know what´s the posible way of creating a new vector from a previous one by subtraction
of the second element to the first one and doing it for all the values of the vector, by
applying Xi-X(i-1)
Use diff
> x <- c(5,2,3,4)
> diff(x)
[1] -3 1 1

How do I subset a dataframe's columns if the data is all the same? [duplicate]

This question already has answers here:
How to remove columns with same value in R
(4 answers)
Closed 2 years ago.
I have a really large dataset and I want to filter out some of the columns because it is the same data all throughout (ex: company name is all "Walmart"). I can go through and do these manually but I'm looking for a code to do it automatically.
I had in mind a function to subset based on if sum(unique(colnam)) == 1 but not sure how to get it to work. Thanks.
which(sapply(dat, function(col) length(unique(col)) == 1))

Change several column into factor - R [duplicate]

This question already has answers here:
Coerce multiple columns to factors at once
(11 answers)
Closed 5 years ago.
I have a data frame with 203 column.
Some of them string and I want to convert them to factor.
like this :
data$var = as.factor((data$var))
The problem is, there is 180 string column. Is there a way to do this with a loop or a sapply maybe ? Anything that can help me to not writing this code 180 times.
I had the same issue to convert into numeric.
The solution for me was to use the following:
data[] <- lapply(data, function(x) as.numeric(as.character(x)))

Mean(or other function) of corresponding elements of a list in R [duplicate]

This question already has answers here:
How to sum a numeric list elements
(2 answers)
Closed 6 years ago.
I have a list, each element in this list is a vector and have same length. I want to calculate the mean(or other value, it can be a user-defined function) of all first element of each vector, mean(or other value, it can be a user-defined function) of all second element of each vector, etc. And return a vector. So this is different from question How to sum a numeric list elements in R .Following code gave me the exactly what I want, however, is there any more efficient and sophistical way to do this? Thanks.
list1 <- list(a=1:5,b=2:6,c=3:7)
result <- numeric(length(list1[[1]]))
for(i in 1:length(list1[[1]])){
result[i] <- mean(c(list1[[1]][i],list1[[2]][i],list1[[3]][i])) #the function can be any other function rather than mean()
}
Here's an option using the Reduce function:
Reduce("+",list1)/length(list1)
[1] 2 3 4 5 6
How about putting them all in a matrix and then calculating the means of the columns?
colMeans(do.call(rbind, list1))
[1] 2 3 4 5 6

very simple subset selection in r [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove rows of a matrix by row name, rather than numerical index?
removing elements in one vector from another in R
I have two vectors:
a<-c(1,2,3,4,5,6,7,8)
b<-c(7,3,6,4,8,1)
I would like to select those elements of a which are not in b
I tried subset(a, a!=b) but I get the warning:
longer object length is not a multiple of shorter object length
Try setdiff for vectors:
R> setdiff(a,b)
[1] 2 5
Try this:
a[!(a%in%b)]
Look at ?"%in%".

Resources