How do I add rows to a column while maintaining index

Okay, so I have created a new column/variable through the help of previously existing one, and now I want to add this variable to my overall data set 'data.combined' however I cannot because the number of rows from my new variable is not big enough so I want to add rows to this variable labeled, 'unknown' . Sadly I also want to maintain index labels as I extracted them from another variable.


a.is.dependant.of.this.for.analysis.reasons <- c(1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1)

a.is.dependant.of.this.for.analysis.reasons <- as.factor(a.is.dependant.of.this.for.analysis.reasons)

a <- c("apple", "cat", "dogs", "penguins", "night", "day", "help", "madness", "grey", "orange", "green", "purple", "blue", "red", "vampires", "hunters", "gracious", "sunglasses", "happiness", "questionable", "scared")

a.data.set <- data.frame(a, a.is.dependant.of.this.for.analysis.reasons)

length.a <- c(a[nchar(a) > 5])

a <- as.factor(a)

length.a <- as.factor(length.a)

# Error ------- below
a.data.set <- data.frame(a, a.is.dependant.of.this.for.analysis.reasons, length.a)

#:

# Error in data.frame(a, a.is.dependant.of.this.for.analysis.reasons, length.a) : 

# arguments imply differing number of rows: 21, 11

What is the purpose of the values in length.a? If you pad it with values of "unknown" and include it in the data frame a.data.set, its values will not have anything to do with the other values in each row. Do you want to have a column that marks whether the column a has 5 or more characters?

I want to grab all the words form 'a' that have over 5 letters and then add those words to another column, in the code above the outcome of 'length.a' would be that it has 11 rows and not the needed 21. When I say adding "unknown" I mean.... take the word 'penguins' it is the third word but when I seperate it along with the others it becomes the first row in the new column, however I want to keep it the third by adding say "unknown" thrice above it to take the place the words, 'apple', 'cat' and 'dogs' as they are below 5 letters.

Is this possible to do?

The ifelse() function will do that.

a.is.dependant.of.this.for.analysis.reasons <- c(1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1)

a.is.dependant.of.this.for.analysis.reasons <- as.factor(a.is.dependant.of.this.for.analysis.reasons)

a <- c("apple", "cat", "dogs", "penguins", "night", "day", "help", "madness", "grey", "orange", "green", "purple", "blue", "red", "vampires", "hunters", "gracious", "sunglasses", "happiness", "questionable", "scared")

a.data.set <- data.frame(a, a.is.dependant.of.this.for.analysis.reasons)

length.a <- ifelse(nchar(a) > 5, a, "unknown")

a <- as.factor(a)

length.a <- as.factor(length.a)

a.data.set <- data.frame(a, a.is.dependant.of.this.for.analysis.reasons, length.a)

head(a.data.set)
#>          a a.is.dependant.of.this.for.analysis.reasons length.a
#> 1    apple                                           1  unknown
#> 2      cat                                           0  unknown
#> 3     dogs                                           0  unknown
#> 4 penguins                                           0 penguins
#> 5    night                                           1  unknown
#> 6      day                                           1  unknown

Created on 2020-01-21 by the reprex package (v0.3.0)

2 Likes

Thank you, so much! :grin:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.