Copy class of columns of one data frame to another data frame [closed]
up vote
0
down vote
favorite
In R how to copy class of columns of one data frame to another data frame? I am building Data Quality Report.
For example I have a data frame x with three columns
col1 clo2 col3
Now i want to copy class of these columns in data frame y as rows
Class
class(col1)
class(col2)
class(col3)
r
New contributor
closed as off-topic by phiver, MLavoie, greg-449, GhostCat, Rob Nov 11 at 14:45
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – phiver, MLavoie, Rob
add a comment |
up vote
0
down vote
favorite
In R how to copy class of columns of one data frame to another data frame? I am building Data Quality Report.
For example I have a data frame x with three columns
col1 clo2 col3
Now i want to copy class of these columns in data frame y as rows
Class
class(col1)
class(col2)
class(col3)
r
New contributor
closed as off-topic by phiver, MLavoie, greg-449, GhostCat, Rob Nov 11 at 14:45
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – phiver, MLavoie, Rob
Welcome to SO! Could you provide us with a sample data set and what the expected outlook would look like?
– Jrakru56
Nov 10 at 15:41
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In R how to copy class of columns of one data frame to another data frame? I am building Data Quality Report.
For example I have a data frame x with three columns
col1 clo2 col3
Now i want to copy class of these columns in data frame y as rows
Class
class(col1)
class(col2)
class(col3)
r
New contributor
In R how to copy class of columns of one data frame to another data frame? I am building Data Quality Report.
For example I have a data frame x with three columns
col1 clo2 col3
Now i want to copy class of these columns in data frame y as rows
Class
class(col1)
class(col2)
class(col3)
r
r
New contributor
New contributor
New contributor
asked Nov 10 at 15:08
Sinchit Rajput
43
43
New contributor
New contributor
closed as off-topic by phiver, MLavoie, greg-449, GhostCat, Rob Nov 11 at 14:45
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – phiver, MLavoie, Rob
closed as off-topic by phiver, MLavoie, greg-449, GhostCat, Rob Nov 11 at 14:45
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – phiver, MLavoie, Rob
Welcome to SO! Could you provide us with a sample data set and what the expected outlook would look like?
– Jrakru56
Nov 10 at 15:41
add a comment |
Welcome to SO! Could you provide us with a sample data set and what the expected outlook would look like?
– Jrakru56
Nov 10 at 15:41
Welcome to SO! Could you provide us with a sample data set and what the expected outlook would look like?
– Jrakru56
Nov 10 at 15:41
Welcome to SO! Could you provide us with a sample data set and what the expected outlook would look like?
– Jrakru56
Nov 10 at 15:41
add a comment |
1 Answer
1
active
oldest
votes
up vote
-1
down vote
Maybe this:
df <- data.frame(a= letters[1:2], b=1, c = as.Date("2018-01-01"))
#list of classes
x<-sapply(df, class)
#Etiher 1 or 2 to get a data.frame of classes
# 1
XX <- as.data.frame(matrix(x, nrow = 1), byrow=TRUE)
names(XX)<- names(x)
XX
#> a b c
#> 1 factor numeric Date
#2
library(tidyverse)
data.frame(x) %>% rownames_to_column("Class") %>% spread(Class,x)
#> a b c
#> 1 factor numeric Date
I don't mind being downvoted but an explanation of why would help me understand why my answer is not correct.
– Jrakru56
Nov 10 at 15:59
Hey, I think your second code makes no sense. df2 will be overridden by the output of sapply(), so the second line is unnecessary .
– Darren Tsai
Nov 10 at 19:32
Right! I was trying to getlist
into thedata.frame
but assigned todf2
instead of the columns in it.
– Jrakru56
Nov 11 at 4:19
I used this and it work thanks: x$class<-sapply(df[0,],class)
– Sinchit Rajput
Nov 11 at 10:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
Maybe this:
df <- data.frame(a= letters[1:2], b=1, c = as.Date("2018-01-01"))
#list of classes
x<-sapply(df, class)
#Etiher 1 or 2 to get a data.frame of classes
# 1
XX <- as.data.frame(matrix(x, nrow = 1), byrow=TRUE)
names(XX)<- names(x)
XX
#> a b c
#> 1 factor numeric Date
#2
library(tidyverse)
data.frame(x) %>% rownames_to_column("Class") %>% spread(Class,x)
#> a b c
#> 1 factor numeric Date
I don't mind being downvoted but an explanation of why would help me understand why my answer is not correct.
– Jrakru56
Nov 10 at 15:59
Hey, I think your second code makes no sense. df2 will be overridden by the output of sapply(), so the second line is unnecessary .
– Darren Tsai
Nov 10 at 19:32
Right! I was trying to getlist
into thedata.frame
but assigned todf2
instead of the columns in it.
– Jrakru56
Nov 11 at 4:19
I used this and it work thanks: x$class<-sapply(df[0,],class)
– Sinchit Rajput
Nov 11 at 10:20
add a comment |
up vote
-1
down vote
Maybe this:
df <- data.frame(a= letters[1:2], b=1, c = as.Date("2018-01-01"))
#list of classes
x<-sapply(df, class)
#Etiher 1 or 2 to get a data.frame of classes
# 1
XX <- as.data.frame(matrix(x, nrow = 1), byrow=TRUE)
names(XX)<- names(x)
XX
#> a b c
#> 1 factor numeric Date
#2
library(tidyverse)
data.frame(x) %>% rownames_to_column("Class") %>% spread(Class,x)
#> a b c
#> 1 factor numeric Date
I don't mind being downvoted but an explanation of why would help me understand why my answer is not correct.
– Jrakru56
Nov 10 at 15:59
Hey, I think your second code makes no sense. df2 will be overridden by the output of sapply(), so the second line is unnecessary .
– Darren Tsai
Nov 10 at 19:32
Right! I was trying to getlist
into thedata.frame
but assigned todf2
instead of the columns in it.
– Jrakru56
Nov 11 at 4:19
I used this and it work thanks: x$class<-sapply(df[0,],class)
– Sinchit Rajput
Nov 11 at 10:20
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Maybe this:
df <- data.frame(a= letters[1:2], b=1, c = as.Date("2018-01-01"))
#list of classes
x<-sapply(df, class)
#Etiher 1 or 2 to get a data.frame of classes
# 1
XX <- as.data.frame(matrix(x, nrow = 1), byrow=TRUE)
names(XX)<- names(x)
XX
#> a b c
#> 1 factor numeric Date
#2
library(tidyverse)
data.frame(x) %>% rownames_to_column("Class") %>% spread(Class,x)
#> a b c
#> 1 factor numeric Date
Maybe this:
df <- data.frame(a= letters[1:2], b=1, c = as.Date("2018-01-01"))
#list of classes
x<-sapply(df, class)
#Etiher 1 or 2 to get a data.frame of classes
# 1
XX <- as.data.frame(matrix(x, nrow = 1), byrow=TRUE)
names(XX)<- names(x)
XX
#> a b c
#> 1 factor numeric Date
#2
library(tidyverse)
data.frame(x) %>% rownames_to_column("Class") %>% spread(Class,x)
#> a b c
#> 1 factor numeric Date
edited Nov 11 at 4:16
answered Nov 10 at 15:47
Jrakru56
51419
51419
I don't mind being downvoted but an explanation of why would help me understand why my answer is not correct.
– Jrakru56
Nov 10 at 15:59
Hey, I think your second code makes no sense. df2 will be overridden by the output of sapply(), so the second line is unnecessary .
– Darren Tsai
Nov 10 at 19:32
Right! I was trying to getlist
into thedata.frame
but assigned todf2
instead of the columns in it.
– Jrakru56
Nov 11 at 4:19
I used this and it work thanks: x$class<-sapply(df[0,],class)
– Sinchit Rajput
Nov 11 at 10:20
add a comment |
I don't mind being downvoted but an explanation of why would help me understand why my answer is not correct.
– Jrakru56
Nov 10 at 15:59
Hey, I think your second code makes no sense. df2 will be overridden by the output of sapply(), so the second line is unnecessary .
– Darren Tsai
Nov 10 at 19:32
Right! I was trying to getlist
into thedata.frame
but assigned todf2
instead of the columns in it.
– Jrakru56
Nov 11 at 4:19
I used this and it work thanks: x$class<-sapply(df[0,],class)
– Sinchit Rajput
Nov 11 at 10:20
I don't mind being downvoted but an explanation of why would help me understand why my answer is not correct.
– Jrakru56
Nov 10 at 15:59
I don't mind being downvoted but an explanation of why would help me understand why my answer is not correct.
– Jrakru56
Nov 10 at 15:59
Hey, I think your second code makes no sense. df2 will be overridden by the output of sapply(), so the second line is unnecessary .
– Darren Tsai
Nov 10 at 19:32
Hey, I think your second code makes no sense. df2 will be overridden by the output of sapply(), so the second line is unnecessary .
– Darren Tsai
Nov 10 at 19:32
Right! I was trying to get
list
into the data.frame
but assigned to df2
instead of the columns in it.– Jrakru56
Nov 11 at 4:19
Right! I was trying to get
list
into the data.frame
but assigned to df2
instead of the columns in it.– Jrakru56
Nov 11 at 4:19
I used this and it work thanks: x$class<-sapply(df[0,],class)
– Sinchit Rajput
Nov 11 at 10:20
I used this and it work thanks: x$class<-sapply(df[0,],class)
– Sinchit Rajput
Nov 11 at 10:20
add a comment |
Welcome to SO! Could you provide us with a sample data set and what the expected outlook would look like?
– Jrakru56
Nov 10 at 15:41