Split data frame string column into multiple columns

I’d like to take data of the form before = data.frame(attr = c(1,30,4,6), type=c(‘foo_and_bar’,’foo_and_bar_2′)) attr type 1 1 foo_and_bar 2 30 foo_and_bar_2 3 4 foo_and_bar 4 6 foo_and_bar_2 and use split() on the column “type” from above to get something like this: attr type_1 type_2 1 1 foo bar 2 30 foo bar_2 3 4 … Read more

Concatenate a vector of strings/character

If I have a vector of type character, how can I concatenate the values into string? Here’s how I would do it with paste(): sdata = c(‘a’, ‘b’, ‘c’) paste(sdata[1], sdata[2], sdata[3], sep =”) yielding “abc”. But of course, that only works if I know the length of sdata ahead of time. 8 Answers 8

How to reshape data from long to wide format

I’m having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name = rep(c(“firstName”, “secondName”), each=4), numbers = rep(1:4, 2), value = rnorm(8) ) dat1 name numbers value 1 firstName 1 0.3407997 2 firstName 2 -0.7033403 3 firstName 3 -0.3795377 4 firstName 4 -0.7460474 5 secondName 1 -0.8981073 6 secondName 2 -0.3347941 7 secondName … Read more

Plotting two variables as lines using ggplot2 on the same graph

A very newbish question, but say I have data like this: test_data <- data.frame( var0 = 100 + c(0, cumsum(runif(49, -20, 20))), var1 = 150 + c(0, cumsum(runif(49, -10, 10))), date = seq(as.Date(“2002-01-01″), by=”1 month”, length.out=100) ) How can I plot both time series var0 and var1 on the same graph, with date on the … Read more