0

As a background. I have two numeric data values (say age and value) and two character data classes (gender and comms). gender is categorical as male and female while comms is categorical as sms and letter.

I tried creating a data frame as follows:

age <- c(mean(age))

value <- c(mean(value))

gender <- c("male", "female")

comms <- c("sms", "letter")

together <- data.frame(age, value, gender, comms)

It gives me an error of arguments imply differing number of rows. Please help on how can I create a dataframe for these data classes.

I want to create an output that looks like

Age     value    gender    comms
24      500       Male      sms
24      500       Female    letter
24      500       Female    sms
24      500       Male      letter
Mikee
  • 783
  • 1
  • 6
  • 18
  • What is the length of `age` and `value`? – Tim Biegeleisen Feb 15 '16 at 07:03
  • Both are of length 1 – Mikee Feb 15 '16 at 07:56
  • 1
    Can you show us what you want your data frame output to look like? – Tim Biegeleisen Feb 15 '16 at 07:57
  • @TimBiegeleisen I've edited my original post to answer your question – Mikee Feb 15 '16 at 08:44
  • 1
    Your output doesn't make any sense to me. Why would you want the first and third rows to be duplicates of each other? – Tim Biegeleisen Feb 15 '16 at 08:45
  • I have no idea what you are trying to accomplish here with that `df` but your code works – Sotos Feb 15 '16 at 08:48
  • @TimBiegeleisen I'm sorry I was not so clear. That is because the age column is only showing the mean of all the ages while the value column is showing the mean of values. The contents of the other columns Gender and Comms are then alternating (in no particular order). I want to put all the values in a data frame before creating another column that will be a reflection of the changing gender and comms – Mikee Feb 15 '16 at 09:00
  • @Sotos It could work but what then happens in a case whereby the comms is of three or more categories? It doesn't work then or am I missing something? – Mikee Feb 15 '16 at 09:01
  • You need to clarify that in your question. However, it sounds to me that you need to make your vectors the same length before binding them to a df. Take a look at this: http://stackoverflow.com/questions/3699405/how-to-cbind-or-rbind-different-lengths-vectors-without-repeating-the-elements-o – Sotos Feb 15 '16 at 09:07
  • @Sotos, I just checked the reference you gave. It does not fill up the empty spaces in the data frame with actual contents as I would want R to, it only just forces them to be same length with empty spaces. – Mikee Feb 15 '16 at 09:39
  • R will recycle shorter vectors _if possible_. Try `data.frame(gender = letters[1:6], val = 1:3)` and `data.frame(gender = letters[1:5], val = 1:3)`. – Roman Luštrik Feb 15 '16 at 20:20

1 Answers1

0

OK, your example is very confusing and it is not clear what you want to do. As I understand it, you could create a list of data.frames and use rbindlist

age <- c(24)

value <- c(400)

gender <- rep_len(c("Male","Female"), length(comms1)) #as suggested by @Jimbou

comms <- c("sms", "letter")

comms1 <- c("sms", "letter", "email", "telephone", "morsecode", "whitedove")

df1 <- data.frame(age, value, gender, comms)
df3 <- data.frame(age, value, gender, comms1)
ls <- list(df1, df3)
rbindlist(ls)

#  age value gender     comms
#1:  24   400   male       sms
#2:  24   400 female    letter
#3:  24   400   male       sms
#4:  24   400 female    letter
#5:  24   400   male     email
#6:  24   400 female telephone
#7:  24   400   male morsecode
#8:  24   400 female whitedove
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • Thanks so much, just what I needed. – Mikee Feb 15 '16 at 10:51
  • 1
    This approach is only working if the length of the vectors are multiples of each. Otherwise the data.frame function gives an error. To extend the vectors you can use `rep_len(c("Male","Female"), length(your_vector))` while `your_vector` specifies the longest one. – Roman Feb 15 '16 at 10:53
  • @Jimbou Great review and extension of the problem, thanks. – Mikee Feb 15 '16 at 11:18
  • 1
    @Sotos, I just checked again and got a message saying I need to have a total of 15 reputation before my check can show. Since I just joined, I don't have the reputation yet. – Mikee Feb 15 '16 at 14:35
  • by the way, just curious, is the `mean(age)` the same in both male and female, or did you just average all ages? – Sotos Feb 15 '16 at 14:50
  • @Sotos, I averaged all the ages. Any further insights? – Mikee Feb 16 '16 at 06:19