Reogranizing the data frame [duplicate] - r

This question already has answers here:
Moving columns within a data.frame() without retyping
(17 answers)
Closed 9 years ago.
I'd like to reorganize my data frame. I just wanted to move the last column into first place and the rest leave in the same order. I used function subset to do it. It works but it would be painful if I have like 100 columns or so.
Is there any easier way to do it ?
tbl_comp <- subset(tbl_comp, select=c("Description","Meve_mean","Mmor_mean", "Mtot_mean", "tot_meanMe", "tot_meanMm", "tot_sdMe", "tot_sdMm", "Wteve_mean", "Wtmor_mean", "Wttot_mean", "tot_meanwte", "tot_meanwtm", "tot_sdwte", "tot_sdwtm"))

Try this
tbl_comp <- subset(tbl_comp, select=c(Description , Meve_mean:tot_sdwtm))

tbl_comp <- cbind(tbl_comp[ncol(tbl_comp)], tbl_comp[-ncol(tbl_comp)])
will do the trick.

Related

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))

Select only numeric variables of a data frame in R [duplicate]

This question already has answers here:
Why does apply convert logicals in data frames to strings of 5 characters?
(2 answers)
Selecting only numeric columns from a data frame
(12 answers)
Closed 2 years ago.
I know that the question is very easy, but I have a more specific one:
I have a data frame, with 50 variables (numeric and non-numeric) and 5000 observations.
Now what I want to do is create another data frame containing only the numerica variables of the original one.
On this website I found the solution of my problem, that is:
numeric_variables<-unlist(lapply(original_data,is.numeric))
X<-original_data[numeric_variables]
But I was wondering: why if I try like this, it does not work instead? what's wrong?
numeric_variables2<-apply(original_data,2,is.numeric)
x<-original_data[numeric_variables2]
try this :
names_num <- names(which(sapply(df, is.numeric)))
df_num <- df[, names_num]

R, Add binary columns based on values in existing column [duplicate]

This question already has answers here:
Generate a dummy-variable
(17 answers)
Closed 5 years ago.
Beginner in R and looking to avoid unnecessary copy+pasting...
I have a data frame with a numeric column. I would like to create binary columns based on the values in the numeric column.
I know the tedious approach would be to copy+paste the following and manually add the different values:
DataFrame$NewCol1 <- as.numeric(DataFrame$ExistingCol == 1);
DataFrame$NewCol2 <- as.numeric(DataFrame$ExistingCol == 2);
Would a "for" loop be able to accomplish this task?
How about something like this?
model.matrix(~factor(DataFrame$ExistingCol))[,-1]

setdiff two single column data frames [duplicate]

This question already has answers here:
Find complement of a data frame (anti - join)
(7 answers)
Closed 7 years ago.
I'm having a problem with a very simple issue and I don't know how to sort it out. Here's the deal. I have two one column data frames
a <- data.frame(C=c("c1","c2","c3","c4","c5","c6","c7","c8"))
b <- data.frame(C=c("c1","c4","c5","c8"))
I would like to get one column dataframe with the entries that do NOT appear in b but they are in a. ie. a dataframe with "c2","c3","c6","c7".
I tried
c <- setdiff(a,b)
but I got the a dataframe and also with
c <- merge(a,b,all.x=TRUE)
I don't get what I want it. so do you know where I am wrong?
We can use anti_join
library(dplyr)
anti_join(a,b)
Or
data.frame(C= setdiff(a[,1], b[,1]))

create data.frame from different number of rows [duplicate]

This question already has answers here:
do.call(rbind, list) for uneven number of column
(4 answers)
Closed 9 years ago.
There are objects with basic datas like a<-list(a=1,b="A",c=character())
Now I want convert it to a data.frame, but there for I need equal rows. How to fill the empty vectors with NA in easy way to run as.data.frame(a)? the only Idea I have is to ask if one elment of the list has length<1 then set element[1]=NA.
I'm not sure this is any cleaner, but it does get rid of the if stuff:
lfoo<-list(one=1:3,two=character(),three=4:6,four=vector())
dfoo<-dfoo<-data.frame(one=rep(NA,3),two=rep(NA,3), three=rep(NA,3),four=rep(NA,3))
lvalues <- which(unlist(lapply(1:4,function(x) length(lfoo[[x]]) > 0))
for (j in lvalues) dfoo[,jvalues]<-lfoo[[jvalues]]
This may point you to simple ways of dealing with conversions and selective replacements.

Resources