Copy class of columns of one data frame to another data frame [closed]









up vote
0
down vote

favorite
2












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)











share|improve this question







New contributor




Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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
If this question can be reworded to fit the rules in the help center, please edit the question.












  • 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














up vote
0
down vote

favorite
2












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)











share|improve this question







New contributor




Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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
If this question can be reworded to fit the rules in the help center, please edit the question.












  • 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












up vote
0
down vote

favorite
2









up vote
0
down vote

favorite
2






2





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)











share|improve this question







New contributor




Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 15:08









Sinchit Rajput

43




43




New contributor




Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Sinchit Rajput is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




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
If this question can be reworded to fit the rules in the help center, please edit the question.




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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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




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












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





share|improve this answer






















  • 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 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


















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





share|improve this answer






















  • 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 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















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





share|improve this answer






















  • 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 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













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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 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 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 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 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




這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto