问题描述
我有一个要转置的数据框(我想转置是正确的单词)。 下面的代码将给出简化的数据框。
tests <- c("TestA","TestB","TestC")
bg <- c(23,13,15)
s1 <- c(21,15,17)
s2 <- c(27,25,11)
s3 <- c(24,14,18)
df <- data.frame(tests,bg,s1,s2,s3)
我想对此进行转置/重塑以使其具有以下结构:
请记住,最多可能有20个测试和50个系列(s1,s2 .... s50)
解决方法
以长格式获取数据,并将变量更改为宽格式:
newlist = sorted(list_of_snaps,key=lambda k: k['snap_id'])
或者使用library(tidyr)
df %>%
pivot_longer(cols = -tests) %>%
pivot_wider(names_from = tests,values_from = value)
# name TestA TestB TestC
# <chr> <dbl> <dbl> <dbl>
#1 bg 23 13 15
#2 s1 21 15 17
#3 s2 27 25 11
#4 s3 24 14 18
data.table
+ dcast
melt
,
标准data.table::transpose
-程序
data.table::transpose( df,make.names = "tests",keep.names = "name" )
# name TestA TestB TestC
# 1 bg 23 13 15
# 2 s1 21 15 17
# 3 s2 27 25 11
# 4 s3 24 14 18
,
Base R单缸纸:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DiaryActivity">
<RelativeLayout
android:id="@+id/save_tap"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/top_line"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
//elements
</RelativeLayout>
<View
android:id="@+id/top_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="#D1D1D1"
app:layout_constraintTop_toBottomOf="@+id/save_tap" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/top_line">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/toolbox">
<EditText
android:id="@+id/edit_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|start"
android:layout_marginTop="20dp"
android:background="@null"
android:hint="@string/hint_diary_title"
android:textColor="@android:color/black"
android:textSize="23sp" />
<EditText
android:id="@+id/edit_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@null"
android:ems="10"
android:gravity="start|top"
android:hint="@string/hint_diary_contents"
android:inputType="textMultiLine"
android:textColor="#515151" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/toolbox"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/top_dividing_line"
android:gravity="center|left"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
//image buttons
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>