在数据框中从长格式转换为宽格式

时间:2020-10-13 01:24:32

标签: r dataframe

我的数据框如下:

dput(Moment[1:15,])
structure(list(SectionCut = c("1", "1", "1", "1", "2", "2", "2", 
"2", "3", "3", "3", "3", "Left", "Left", "Left"), N_l = c("1", 
"2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4", "1", "2", 
"3"), UG = c("84", "84", "84", "84", "84", "84", "84", "84", 
"84", "84", "84", "84", "84", "84", "84"), S = c("12", "12", 
"12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12", 
"12", "12"), Sample = c("S00", "S00", "S00", "S00", "S00", "S00", 
"S00", "S00", "S00", "S00", "S00", "S00", "S00", "S00", "S00"
), DF = c(0.367164093630677, 0.540130283330855, 0.590662743113521, 
0.497030982705986, 0.000319303760901125, 0.000504925126205843, 
0.00051127115578891, 0.000395434233037301, 0.413218926236695, 
0.610726262711904, 0.685000816613652, 0.59474035159783, 0.483354599644366, 
0.645710184115934, 0.625883097885242)), row.names = c(NA, -15L
), class = c("tbl_df", "tbl", "data.frame"))

我想通过旋转SectionCut列来分离列的内容。我基本上希望以某种方式使用pivot_longer的反面...因此,最后,DF列中的值将显示在5个不同的列下(SectionCut = c("1", "2", "3", "left", "right")

的值

1 个答案:

答案 0 :(得分:1)

在使用#include <iostream> using namespace std; class A { public: string value; A() { value = "ABC"; } void print_values() { std::cout << "value: " << value << std::endl; } }; class B { A& a_prop; public: B(A& a) : a_prop(a) { } void print_values() { a_prop.print_values(); } }; int main() { auto objectA = std::make_unique<A>(); auto objectB = std::make_unique<B>(*objectA); objectA->value.replace(objectA->value.begin(), objectA->value.end() - 1, "ED"); objectA->print_values(); // Will display "EDC" objectB->print_values(); // Will display "EDC" auto objectAMoved = std::move(objectA); objectAMoved->print_values(); // Will display "EDC" objectAMoved->value.replace(objectAMoved->value.begin(), objectAMoved->value.end() - 1, "FG"); objectB->print_values(); // Will display "FGC" objectAMoved->print_values(); // Will display "FGC" if (objectA == nullptr) { cout << "A is nullptr" << endl; } else { objectA->print_values(); // This will cause segmentation fault segmentation fault } } 创建序列列之后,我们可以使用import { useState, useEffect } from 'react'; import { firestore } from '../firebase/firebase'; const useFirestore = (collection) => { const [docs, setDocs] = useState([]); useEffect(() => { const unsub = firestore.collection(collection) .orderBy('createdAt', 'desc') .onSnapshot(snap => { let documents = []; snap.forEach(doc => { documents.push({...doc.data(), id: doc.id}); }); setDocs(documents); }); return () => unsub(); // this is a cleanup function that react will run when // a component using the hook unmounts }, [collection]); return { docs }; } export default useFirestore 中的import * as firebase from "firebase/app" import "firebase/firestore" const firebaseConfig = { "this is filled with info I got from firebase just dont want to post it on the internet." }; const firestore = firebase.initializeApp(firebaseConfig); export { firestore }

pivot_wider

-输出

tidyr