Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Virgile Baudrot
fdf
Commits
e299ab62
Commit
e299ab62
authored
Oct 24, 2019
by
Virgile Baudrot
Browse files
update melt function key is LIST
parent
d708a6e6
Changes
1
Hide whitespace changes
Inline
Side-by-side
R/melt.R
View file @
e299ab62
...
...
@@ -53,7 +53,7 @@ fd_melt_SINGLE <- function(x, key1, id = NULL){
#'
#' @export
#'
fd_melt
<-
function
(
x
,
key1
,
key2
=
NULL
,
id
=
NULL
,
keep
=
NULL
){
fd_melt
_12
<-
function
(
x
,
key1
,
key2
=
NULL
,
id
=
NULL
,
keep
=
NULL
){
if
(
is.null
(
key2
)){
DF
=
fd_melt_SINGLE
(
x
,
key1
,
id
)
...
...
@@ -70,6 +70,45 @@ fd_melt <- function(x, key1, key2 = NULL, id = NULL, keep = NULL){
return
(
DF
)
}
#' @name fd_melt
#'
#' @param key a list or vector of column name to keep
#'
#' @export
#'
fd_melt
<-
function
(
x
,
key
,
id
=
NULL
,
keep
=
NULL
){
lgth_key
=
lapply
(
1
:
length
(
key
),
function
(
i
){
sapply
(
x
[[
key
[[
i
]]]],
length
)
})
if
(
length
(
lgth_key
)
>
1
){
if
(
!
all
(
sapply
(
2
:
length
(
lgth_key
),
function
(
i
)
lgth_key
[[
1
]]
==
lgth_key
[[
i
]])
))
{
stop
(
"length of element within 'key' differ."
)}
}
if
(
is.null
(
id
))
{
id
=
1
:
length
(
lgth_key
[[
1
]])
}
else
{
id
=
x
[[
id
]]
}
DF
=
data.frame
(
id
=
do.call
(
"c"
,
lapply
(
1
:
length
(
id
),
function
(
i
)
rep
(
id
[
i
],
lgth_key
[[
1
]][[
i
]])))
)
for
(
i
in
1
:
length
(
key
)){
DF
[[
key
[[
i
]]]]
=
do.call
(
"c"
,
x
[[
key
[[
i
]]]])
}
# --- keep
if
(
!
is.null
(
keep
))
{
for
(
i
in
1
:
length
(
keep
)){
DF
[[
keep
[[
i
]]]]
=
fd_melt_STICK
(
x
,
key
[[
1
]],
keep
[[
i
]])
}
}
return
(
DF
)
}
#' @name fd_melt
#'
#' @export
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment