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.
Related
This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 1 year ago.
I have this data and I want to figure out a way to know how many ones and how many zeros are in each column (ie Arts and Crafts). I have been trying different things but it hasn't been working. Does anyone have any suggestions?
You can use the table() function in R. This creates a categorical representation of your data. Additionally here convert list to vector I have used unlist() function.
df1 <- read.csv("Your_CSV_file_name_here.csv")
table(unlist(df1$ArtsAndCrafts))
If you want to row vice categorize the number of zeros and ones you can refer to this question in Stackoverflow.
This question already has answers here:
How to generate all possible combinations of vectors without caring for order?
(2 answers)
Closed 3 years ago.
I have multiple character strings, let's say 'pred_1', 'pred_2' and 'pred_3'. Now I want to get a list with all pairs of the strings. So, the resulting list should contain 'pred_1 pred_2', 'pred_1 pred_3' and 'pred_2 pred_3'. Does anyone know how to automate this for more than three character strings?
An option is combn
combn(v1, 2, simplify = FALSE)
data
v1 <- paste0("pred_", 1:3)
This question already has answers here:
How do you create a list with a single value in R?
(2 answers)
Closed 5 years ago.
Is there a more elegant way to create a list consisting only of zeros (I don't want to use vector("list", <len>) because it creates NULL entries)?
lapply((1:5), function(x) 0)
(Sorry, this is probably a duplicate, but I couldn't find the answer ..)
Thx & kind regards
We can use the as.list with numeric or integer
as.list(numeric(5))
We can use rep like this
rep(0,5)
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]
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.