Reputation: 31
dataframe column component region, x1 mean, x2 mean, x1 se, x2 se.
I want changed dataframe for line graph (ggplot)
finally, i make column region, part, mean, se. (part is new column)
how to use code ?
#data.frame = temp
region x1 x2 y1 y2
A 1 2 0.1 0.2
B 3 4 0.3 0.2
#modified data.frame
region part mean se
A x1 1 0.1
A X2 2 0.2
B X1 3 0.3
B X2 4 0.2
please help me..
Upvotes: 0
Views: 88
Reputation: 887851
We can use melt
from data.table
which can take multiple columns to reshape from 'wide' to 'long' within the measure
argument
library(data.table)
melt(setDT(temp), measure = patterns("^x\\d+", "^y\\d+"),
value.name = c('mean', 'se'), variable.name = 'part')[,
part := paste0("X", part)][order(region)]
# region part mean se
#1: A X1 1 0.1
#2: A X2 2 0.2
#3: B X1 3 0.3
#4: B X2 4 0.2
Or we can use tidyverse
as it is useful for piping with ggplot
library(ggplot2)
gather(temp, key, val, x1:y2) %>%
separate(key, into = c('key1', 'part'), sep="(?<=[a-z])(?=[0-9])") %>%
spread(key1, val) %>%
rename_at(3:4, ~ c('mean', 'se')) %>%
mutate(part = paste0("X", part))
# region part mean se
#1 A X1 1 0.1
#2 A X2 2 0.2
#3 B X1 3 0.3
#4 B X2 4 0.2
Upvotes: 1
Reputation: 79338
using Base R:
A = reshape(dat,t(matrix(2:5,2)),idvar = 1,dir="long",ids=dat$region)
setNames(transform(A,time=paste0("X",time),row.names=NULL)[order(A$region),],
c(names(dat)[1],"Part","mean","se"))
region Part mean se
1 A X1 1 0.1
3 A X2 2 0.2
2 B X1 3 0.3
4 B X2 4 0.2
Upvotes: 1